这篇文章主要介绍了C#实现文件上传下载Excel文档示例代码,需要的朋友可以参考下
要求
环境信息:WIN2008SERVER 开发工具:VS2015 开发语言:C#
要求:
1.点击同步数据后接口获取数据展示页面同时过滤无效数据并写入数据库,数据可导出Excel并支持分类导出
2.Excel导入确认数据,调用服务处理数据后写入数据库,并支持分类导出
这两天搞了一个小功能,其他的不说了针对Excel导入导出做一个小总结
导出文件
这里的文件导出是底层写好的,个人理解有限而且毕竟属于公司就不贴具体代码了,简单说一下思路
首先是建立导出Excel管理类,用于管理Excel文件导出的模板 样式 每行的计算方式等等,当然需要在项目中添加该管理类的配置文件去匹配对应模板;
1.读取对应配置文件,获取配置文件模板信息 至于模板如何配置就不说啦xml文件操作园子里面很多篇关于这个文章
导入文件
导入文件首先需要上传,文件上传至服务器指定地址之后再去针对服务器文件解析,其实原理很简单,就是通过解析上传的文件通过OLDB方式获取解析后的文件DataSet然后在写入数据库,下面是一个上传文件格式验证
public static ReturnValue ReadExcelToDataSet(string xlsFullFileName, bool isHDR, bool isIMEX, int limitSheetCount, bool isOnlyVerify)
{
ReturnValue returnValue = new ReturnValue();
string fileExt = UploadFileUtils.GetFileExt(xlsFullFileName);
if (string.IsNullOrEmpty(fileExt) || !StringUtils.IsInLimitStr("xls,xlsx", fileExt))
{
returnValue.HasError = true;
returnValue.ReturnCode = 1;
returnValue.Message = "无效excel文件后缀";
return returnValue;
}
if (!File.Exists(xlsFullFileName))
{
returnValue.HasError = true;
returnValue.ReturnCode = 2;
returnValue.Message = "文件不存在";
return returnValue;
}
StringBuilder stringBuilder = new StringBuilder();
string str;
if ("xlsx".Equals(fileExt, StringComparison.CurrentCultureIgnoreCase))
{
stringBuilder.Append("Provider=Microsoft.ACE.OLEDB.12.0");
str = "Excel 12.0;";
}
else
{
stringBuilder.Append("Provider=Microsoft.Jet.OLEDB.4.0");
str = "Excel 8.0;";
}
stringBuilder.Append(";Data Source=" + xlsFullFileName);
stringBuilder.Append(";Extended Properties=\"" + str);
if (isHDR)
{
stringBuilder.Append(";HDR=Yes");
}
if (isIMEX)
{
stringBuilder.Append(";IMEX=1");
}
stringBuilder.Append("\"");
ExcelUtils.log.Debug(stringBuilder.ToString());
OleDbConnection oleDbConnection = new OleDbConnection(stringBuilder.ToString());
try
{
oleDbConnection.Open();
DataTable oleDbSchemaTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[]
{
null,
null,
null,
"Table"
});
if (oleDbSchemaTable == null || oleDbSchemaTable.Rows.Count == 0)
{
returnValue.HasError = true;
returnValue.ReturnCode = 3;
returnValue.Message = "读取不到sheet的信息";
ReturnValue result = returnValue;
return result;
}
if (isOnlyVerify)
{
returnValue.HasError = false;
returnValue.Message = "读取sheet信息正确";
returnValue.PutValue("dtSheet", oleDbSchemaTable);
ReturnValue result = returnValue;
return result;
}
int num = oleDbSchemaTable.Rows.Count;
if (limitSheetCount > 0 && limitSheetCount < oleDbSchemaTable.Rows.Count)
{
num = limitSheetCount;
}
string[] array = new string[num];
for (int i = 0; i < num; i++)
{
array[i] = oleDbSchemaTable.Rows[i]["TABLE_NAME"].ToString();
}
DataSet dataSet = new DataSet();
for (int j = 0; j < num; j++)
{
string text = "select * from [" + array[j] + "]";
ExcelUtils.log.Debug(text);
OleDbCommand selectCommand = new OleDbCommand(text, oleDbConnection);
OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(selectCommand);
DataTable dataTable = new DataTable(array[j]);
oleDbDataAdapter.Fill(dataTable);
dataSet.Tables.Add(dataTable);
}
returnValue.HasError = false;
returnValue.PutValue("dtSheet", oleDbSchemaTable);
returnValue.PutValue("arrTableName", array);
returnValue.ReturnObject = dataSet;
returnValue.Message = "读取成功";
}
catch (Exception ex)
{
returnValue.HasError = true;
returnValue.ReturnCode = -100;
returnValue.ReturnException = ex;
returnValue.Message = ex.Message;
ExcelUtils.log.WarnFormat("ReadExcelToDataSet sbConn={0} 出错,原因:{1}", stringBuilder.ToString(), ex.Message);
}
finally
{
oleDbConnection.Close();
}
return returnValue;
}
哦对 注意一下,如果用导出方法导出xls文件再用导入方法导入该文件会报错的哟,我是默认保存.csv 实际就为了确定文件是否被使用过,所以当你的excel文件能导出单相同文件却导入不了 请尝试一下重新保存一下.xls格式 在进行导入
本文标题为:C#实现文件上传下载Excel文档示例代码
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- 如何使用C# 捕获进程输出 2023-03-10
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- Oracle中for循环的使用方法 2023-07-04
- Unity Shader实现模糊效果 2023-04-27
- c# 模拟线性回归的示例 2023-03-14
- .NET CORE DI 依赖注入 2023-09-27
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- Unity3D实现渐变颜色效果 2023-01-16
- user32.dll 函数说明小结 2022-12-26