C#的NPOI根据模板导出动态EXCEL图表,

时间:2021-10-30 01:10:56

对于数据导出网上数不胜数,可是图表却寥寥无几,可能我没搜到吧。。。。

这个方法感觉对于EXCEL模板的要求比较高,模板做的好导出来的效果相对完美一点,导出的数据若是动态行列的不妨看下如下这篇,先做好一份好的模板

https://www.zhihu.com/question/48727312/answer/113147034

顺便再放一个纵向折线图画法

http://www.officedoyen.com/a/exceltubiao/yibantubiao/zhexiantu/2015/0714/10795.html

接下来就是一些代码了

js代码,写一个点击事件访问后台方法,后面跟自己要加的参数

//导出点击事件
    $("#btExport").click(function () {
      
        window.open("/text/ajax/export.aspx?Method=export&language=" +c.language);
    });

后台代码

   
 DBClass db = new DBClass();      MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook(); //模板路径,从ajax的上一层test找到excel文件下的模板.xls
        string excelTempPath = Server.MapPath("~/test/") + "/excel/模板.xls"; //读取Excel模板
        using (FileStream fs = new FileStream(excelTempPath, FileMode.Open, FileAccess.Read)) { workbook = new HSSFWorkbook(fs); }       ISheet sheet = workbook.GetSheetAt(0);//表示在模板的第一个工作簿里写入
        IRow headerRow = sheet.CreateRow(0); try
        {
            //string starttime = Request.Params["Ssss"].ToString();//后台取出传来的数据
            //倘若是ajax的方式传值用MyForm["Method"].ToString().ToLower()
            //读取数据库的名称
            DataTable newTable = new DataTable();
            SqlCommand comm;
            DataSet ds;
            string sql = "这里写取数据的sql"
            comm = db.getSqlCommand(sql);
            ds = db.getDataSetBySqlCommand(comm);
        //下面这部分一般人不需要可以删掉
            newTable = ds.Tables[0].Copy();
            DataRow dr = newTable.NewRow();
            int BSum = 0;
            int PNum = 0;

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                BSum += int.Parse(ds.Tables[0].Rows[i][1].ToString());
                PNum += int.Parse(ds.Tables[0].Rows[i][2].ToString());
            }
            object[] objs = { "业务部", BSum, PNum };
            dr.ItemArray = objs;
            newTable.Rows.Add(dr);
        //新插入一行数据
            DataTable table = newTable;

            int k = 0;
            string language = Request.Params["language"].ToString();
    //以下还是有必要的加上头部名称
foreach (DataColumn column in table.Columns) {
        //  headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption)//加上为列名的头部  
          if (language == "zh-cn") {
            //设置自定义头部名称 headerRow.CreateCell(column.Ordinal).SetCellValue(deptbusinessTitle[k].Split(
',')[0]); } else { headerRow.CreateCell(column.Ordinal).SetCellValue(deptbusinessTitle[k].Split(',')[1]); } k++; } int rowIndex = 1; foreach (DataRow row in table.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); int colnum = 0; foreach (DataColumn column in table.Columns) { if (colnum == 0)//导入excel时一些数据列必须是int类型的,否则它就不会自己绘制 { dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } else { dataRow.CreateCell(column.Ordinal).SetCellValue(int.Parse(row[column].ToString())); } colnum++; } rowIndex++; } workbook.Write(ms); ms.Flush(); ms.Position = 0; string fileName = "导出文件.xls"; if (Request.Browser.Browser == "IE") fileName = HttpUtility.UrlEncode(fileName); Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName); Response.BinaryWrite(ms.ToArray()); } catch (Exception e) { throw e; } finally { db.CloseConn(); }