C# 根据模板 导出 Excel 图表 (NPOI组件)
C# 根據(jù)模板 導(dǎo)出 ?Excel ?圖表 (NPOI組件)
npoi ?版本2.2.1?
數(shù)據(jù)存在List中的。 ??
?using NPOI.SS.UserModel;
?using NPOI.SS.Util;
?using NPOI.HSSF.UserModel;
using System.IO;
//導(dǎo)出
? ? ? ? protected void onclickbutton(object sender, EventArgs e)? ? ? ? {
? ? ? ? ? ? getlist();//取出數(shù)據(jù),就不詳細(xì)說明
? ? ? ? }
? ?public bool ExportGJExcelFromList1(List<VW_ProductorOrderItem> lists, String FileName)
? ? ? ? {
? ? ? ? ? ?
? ? ? ? ? ? NPOI.HSSF.UserModel.HSSFWorkbook hssfworkbook = new HSSFWorkbook();
? ? ? ? ? ? string strMouldPath = Server.MapPath("~") + @"\Mould\tongji\xiaoliang.xlt";
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? FileStream fs = File.OpenRead(strMouldPath);
? ? ? ? ? ? ? ? hssfworkbook = new HSSFWorkbook(fs);
? ? ? ? ? ? ? ? fs.Close();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? throw e;
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ?
? ? ? ? ? ? NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
? ? ? ?
? ? ? ? ? ? int RowsNum = 1;
? ? ??
? ? ? ? ? ?
? ? ? ? ? ? for (int i = 0; i < lists.Count; i++)
? ? ? ? ? ? {?
? ? ? ? ??
? ? ? ? ? ? ? ? sheet.ShiftRows(RowsNum + i, sheet.LastRowNum, 1, true, false);
? ? ? ? ? ? ? ? NPOI.SS.UserModel.IRow row = sheet.CreateRow(RowsNum + i);
? ? ? ? ? ? ? ? row.CreateCell(0).SetCellValue(i + 1);
? ? ? ? ? ? ? ? row.CreateCell(1).SetCellValue(lists[i].TargetOfYear);
? ? ? ? ? ? ? ? row.CreateCell(2).SetCellValue(lists[i].OrderDate);
? ? ? ? ? ? ? ? Double a = Convert.ToDouble(lists[i].Price);//這是我模板選擇的值,如果直接取出值,則圖表不會(huì)顯示,因?yàn)檩敵龅氖亲址皇菙?shù)字,識(shí)別不了。
? ? ? ? ? ?
? ? ? ? ? ? ? ? row.CreateCell(3).SetCellValue(a);
? ? ? ? ? ? ?? row.CreateCell(4).SetCellValue("RMB");
? ? ? ? ? ? ? ? row.CreateCell(5).SetCellValue(lists[i].Rate);
? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? sheet.ShiftRows(RowsNum + lists.Count, sheet.LastRowNum, 1, true, false);
? ? ? ? ? ? ? ? ? NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(RowsNum + lists.Count);
? ? ? ? ? ? ? ? ? double b=0;
? ? ? ? ? ? ? ? ? for (int i = 0; i < lists.Count; i++)
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? double ?a = Convert.ToDouble ( lists[i].Price);
? ? ? ? ? ? ? ? ? ? ? b = a + b;
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? row1.CreateCell(0).SetCellValue("合計(jì)");
? ? ? ? ? ? ? ? ? row1.CreateCell(1).SetCellValue(b);
? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? sheet.ForceFormulaRecalculation = true;
?
? ? ? ? ? ? System.IO.MemoryStream ms = new System.IO.MemoryStream();
? ? ? ? ? ? hssfworkbook.Write(ms);
? ? ? ? ? ? FileName = HttpUtility.UrlEncode(FileName, Encoding.UTF8);
? ? ? ? ? ? HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", FileName));
? ? ? ? ? ? HttpContext.Current.Response.BinaryWrite(ms.ToArray());
? ? ? ? ? ? hssfworkbook = null;
? ? ? ? ? ? ms.Close();
? ? ? ? ? ? ms.Dispose();
? ? ? ? ? ? return true;
}
//模板地址,.xls要另存為.xlt
//模板
? ? ? ? ??
//最后導(dǎo)出的樣式
總結(jié)
以上是生活随笔為你收集整理的C# 根据模板 导出 Excel 图表 (NPOI组件)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gc就是fullgc吗 major_线上
- 下一篇: bean validation校验方法参