使用使用Aspose插件對Excel文檔進行導入導出操作
使用前請先下載Aspose插件引用
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);
}
return File(fullFileName, "application/ms-excel", UserName + "_Test單" + newFilename);// 方法Action裡直接返回File文件下載。
/// <summary>
//DataTable數據源導出,封裝ToExcel方法。
da.ToExcel(fullFileName);
return File(fullFileName, "application/ms-excel", UserName + "_Test單" + newFilename);
由於抽取部分代碼出來,排版和引用方面未做詳細注釋,前台和後台方法對應即可。
做個筆記日後方便自己查看使用。做個筆記日後方便自己查看使用。做個筆記日後方便自己查看使用。做個筆記日後方便自己查看使用。