使用NPOI和委托做EXCEL导出
生活随笔
收集整理的這篇文章主要介紹了
使用NPOI和委托做EXCEL导出
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
首先,在用NPOI導(dǎo)出時(shí),學(xué)習(xí)了邀月這篇文章NPOI根據(jù)Excel模板生成原生的Excel文件實(shí)例,在這里先行謝過(guò)了。
本篇文章在邀月的基本上,做了一些小的改動(dòng),加上委托的機(jī)制。因?yàn)樵谧鰧?dǎo)出時(shí),加載模板,下載為EXCEL的代碼相同,但是在設(shè)置EXCEL模板中的值時(shí)有很大的不同。所以以了一些小的改動(dòng)。
改動(dòng)后的主要類文件如下:
ExcelHelper:
View Code public class ExcelHelper{private string templatePath;private string newFileName;private string templdateName;private string sheetName;public string SheetName{get { return sheetName; }set { sheetName = value; }}public ExcelHelper(string templdateName, string newFileName){this.sheetName = "sheet1";templatePath = HttpContext.Current.Server.MapPath("/") + "/Config/Template/";this.templdateName = string.Format("{0}{1}", templatePath, templdateName);this.newFileName = newFileName;}public void ExportDataToExcel(Action<HSSFSheet> actionMethod){using (MemoryStream ms = SetDataToExcel(actionMethod)){byte[] data = ms.ToArray();#region response to the clientHttpResponse response = System.Web.HttpContext.Current.Response;response.Clear();response.Charset = "UTF-8";response.ContentType = "application/vnd-excel";//"application/vnd.ms-excel";System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=" + newFileName));System.Web.HttpContext.Current.Response.BinaryWrite(data);#endregion}}private MemoryStream SetDataToExcel(Action<HSSFSheet> actionMethod){//Load template fileFileStream file = new FileStream(templdateName, FileMode.Open, FileAccess.Read);HSSFWorkbook workbook = new HSSFWorkbook(file);HSSFSheet sheet = workbook.GetSheet(SheetName);if (actionMethod != null) actionMethod(sheet);sheet.ForceFormulaRecalculation = true;using (MemoryStream ms = new MemoryStream()){workbook.Write(ms);ms.Flush();ms.Position = 0;sheet = null;workbook = null;return ms;}}}PrintManager:
View Code public class PrintManager{public void PrintPurchase(){ExcelHelper helper = new ExcelHelper("PurchaseOrder.xls", "PurchaseOrder_C000001.xls");helper.ExportDataToExcel(SetPurchaseOrder);}private void SetPurchaseOrder(HSSFSheet sheet){HSSFRow row = null;HSSFCell cell = null;row = sheet.GetRow(2);cell = row.GetCell(1);cell.SetCellValue("C0000001");cell = row.GetCell(7);cell.SetCellValue("2013-04-18");DataTable itemDT = PrepareItemDTForTest();SetDataTableValue(sheet, 7, 0, itemDT);row = sheet.GetRow(14);cell = row.GetCell(0);cell.SetCellValue("NOKIA");cell = row.GetCell(6);cell.SetCellValue("CMCC");}public void SetDataTableValue(HSSFSheet sheet, int rowIndex, int columnIndex,DataTable dt) {HSSFRow row = null;HSSFCell cell = null;foreach (DataRow dataRow in dt.Rows){row = sheet.GetRow(rowIndex);columnIndex = 0;foreach (DataColumn column in dt.Columns){cell = row.GetCell(columnIndex);string drValue = dataRow[column].ToString();switch (column.DataType.ToString()){case "System.String":cell.SetCellValue(drValue);break;case "System.DateTime":DateTime dateV;DateTime.TryParse(drValue, out dateV);cell.SetCellValue(dateV);break;case "System.Boolean":bool boolV = false;bool.TryParse(drValue, out boolV);cell.SetCellValue(boolV);break;case "System.Int16":case "System.Int32":case "System.Int64":case "System.Byte":int intV = 0;int.TryParse(drValue, out intV);cell.SetCellValue(intV);break;case "System.Decimal":case "System.Double":double doubV = 0;double.TryParse(drValue, out doubV);cell.SetCellValue(doubV);break;case "System.DBNull":cell.SetCellValue("");break;default:cell.SetCellValue("");break;}columnIndex++;}rowIndex++;}}private DataTable PrepareItemDTForTest() {DataTable itemDT = new DataTable();itemDT.Columns.Add("Name");itemDT.Columns.Add("Qty", Type.GetType("System.Decimal"));itemDT.Columns.Add("UnitPrice", Type.GetType("System.Decimal"));DataRow newRow = itemDT.NewRow();newRow[0] = "820";newRow[1] = "100";newRow[2] = "3000";itemDT.Rows.Add(newRow);DataRow newRow2 = itemDT.NewRow();newRow2[0] = "920";newRow2[1] = "100";newRow2[2] = "4000";itemDT.Rows.Add(newRow2);return itemDT;}}備注:
ExcelHelper類:負(fù)責(zé)打開模板,調(diào)用傳來(lái)的方法設(shè)置值,輸出流。
PrintManager類:負(fù)責(zé)調(diào)用ExcelHelper然后到業(yè)務(wù)模塊取數(shù)據(jù),然后將業(yè)務(wù)數(shù)據(jù)填充到HSSFSheet中去。
?
效果
EXCEL模板:
導(dǎo)出的EXCEL:
DEMO下載:NopiTest.zip
轉(zhuǎn)載于:https://www.cnblogs.com/dataadapter/archive/2013/04/18/3029433.html
總結(jié)
以上是生活随笔為你收集整理的使用NPOI和委托做EXCEL导出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: $_SERVER
- 下一篇: DropBox 超实用的免费文件网络同步