使用使用Aspose插件对Excel文档进行导入导出操作
Excel导入:
前台使用file标签获取,submit方式提交。
<form id="form1" enctype="multipart/form-data" method="post" > <table class="table-condensed"> <tr> <td class="text-right">导入表格:</td> <td class="text-left"><input type="file" name="file1" class="btn btn-default btn-lg" /></td> </tr> <tr> <td class="text-left"> <input type="submit" id="btnImport" name="btnImport" value="导入" class="btn btn-default" /> </td> </tr> </table> </form>
后台接收:
HttpPostedFileBase fileBase = Request.Files["file1"];//这里获取名称与前台标签name保持一致 if (fileBase != null) { string filename = Path.GetFileName(fileBase.FileName); string extension = Path.GetExtension(filename); string path = "/Upload/Test/" + DateTime.Now.ToString("yyyyMMdd") + "/"; Directory.CreateDirectory(Path.GetDirectoryName(Request.MapPath(path))); string newFilename = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string fullFileName = path + newFilename + extension; fileBase.SaveAs(Request.MapPath(fullFileName)); try { Stopwatch sw = new Stopwatch();//记录导入操作用时多长 sw.Start(); //这里可放入BLL方法处理 string result = new ProductBLL().ImportExcel(Request.MapPath(path), newFilename, extension); //BLL方法 ProductBLL public string ImportExcel(string path, string filename, string extension)
{
Workbook workbook = new Workbook(path + filename + extension);
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
for (int i = 1; i < cells.Rows.Count; i++)
{
try
{
string brand = cells[i, 0].StringValue.Trim();//获取列值
string years = cells[i, 1].StringValue.Trim();
}
catch (Exception e)
{
continue;
}
}
return "OK";
} sw.Stop(); long runTime = sw.ElapsedMilliseconds / 1000; } catch (Exception e) { Log.Write("导入", "导入错误", "错误信息:" + e.Message); } }
Excel导出:
string path = "/Upload/Test/" + DateTime.Now.ToString("yyyyMMdd") + "/"; Directory.CreateDirectory(Path.GetDirectoryName(Server.MapPath(path))); string newFilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; string fullFileName = Server.MapPath(path + newFilename); public void ExportInfo(List<Test> list, string fullFileName) { Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(); Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0]; cellSheet.PageSetup.LeftMargin = 0.3; cellSheet.PageSetup.RightMargin = 0.3; cellSheet.PageSetup.TopMargin = 1; cellSheet.PageSetup.BottomMargin = 0.5; cellSheet.PageSetup.FooterMargin = 0.5; cellSheet.PageSetup.HeaderMargin = 0.5; cellSheet.PageSetup.Orientation = PageOrientationType.Landscape; cellSheet.PageSetup.CenterHorizontally = true; cellSheet.PageSetup.CenterVertically = true; cellSheet.Cells[0, 0].PutValue("货号"); cellSheet.Cells[0, 1].PutValue("颜色"); cellSheet.Cells[0, 2].PutValue("尺码"); int i = 1; foreach (var item in list) { cellSheet.Cells[i, 0].PutValue(item.productno); cellSheet.Cells[i, 1].PutValue(item.size); cellSheet.Cells[i, 2].PutValue(item.color); i++; } cellSheet.AutoFitColumns(); fullFileName = Path.GetFullPath(fullFileName); workbook.Save(fullFileName); }
由于抽取部分代码出来,排版和引用方面未做详细注释-----------------------------------------------------------
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
2223222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
3333333333333333333233333333333333333333333333333333333333333333333333333333333333333333333333333333
啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦
啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦
啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦
啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦
啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦 啦啦啦啦啦啦啦啦啦啦啦啦