zoukankan      html  css  js  c++  java
  • NOPI导出Excel

                DataTable dt = GetMonthQsEditData(mainId);//数据  
                using (FileStream file = new FileStream(HttpContext.Current.Server.MapPath("/Files/Templetes/月清算模板.xls"), FileMode.Open, FileAccess.Read))
                {
                    HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
                    ISheet sheet = hssfworkbook.GetSheetAt(0);
                    int startRow = 2;//开始行
                    int count = 0;
                    List<int> merg = new List<int>();
                    string unitId = "";
                    IRow row = null;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (i == 0)
                        {
                            row = sheet.GetRow(startRow);
                        }
                        else
                        {
                            row = CopyRow(hssfworkbook, sheet, startRow, startRow + i,false);
                        }
    
                        if (unitId != dt.Rows[i]["UNITID"].ToString())
                        {
                            if (count != 0)
                            {
                                merg.Add(count);
                            }
                            count = 0;
                            row.GetCell(0).SetCellValue(dt.Rows[i]["UNITNAME"].ToString());
                            row.GetCell(1).SetCellValue(dt.Rows[i]["SFNAME"].ToString());
                            row.GetCell(2).SetCellValue(dt.Rows[i]["SFID"].ToString());
                            row.GetCell(3).SetCellValue(string.Format("{0:0.0}", dt.Rows[i]["YEARTOTAL"]));
                            row.GetCell(4).SetCellValue(string.Format("{0:0.0}", dt.Rows[i]["MONTHPROPORTION"]));
                            row.GetCell(5).SetCellFormula("D" + (3 + i) + "*E" + (3 + i) + "/100");
                            row.GetCell(6).SetCellValue(string.Format("{0:0.0}", dt.Rows[i]["ACTUAL"]));
                            unitId = dt.Rows[i]["UNITID"].ToString();
                        }
                        else
                        {
                            row.GetCell(0).SetCellValue(dt.Rows[i]["UNITNAME"].ToString());
                            row.GetCell(1).SetCellValue(dt.Rows[i]["SFNAME"].ToString());
                            row.GetCell(2).SetCellValue(dt.Rows[i]["SFID"].ToString());
                            row.GetCell(3).SetCellValue(string.Format("{0:0.0}", dt.Rows[i]["YEARTOTAL"]));
                            row.GetCell(4).SetCellValue(string.Format("{0:0.0}", dt.Rows[i]["MONTHPROPORTION"]));
                            row.GetCell(5).SetCellFormula("D" + (3 + i) + "*E" + (3 + i) + "/100");
                            row.GetCell(6).SetCellValue(string.Format("{0:0.0}", dt.Rows[i]["ACTUAL"]));
                        }
                        count++;
                    }
                    int rowNum = 2;
                    foreach (int v in merg)
                    {
                        CellRangeAddress cellRangeAddress = new CellRangeAddress(rowNum, rowNum + v - 1, 0, 0);
                        sheet.AddMergedRegion(cellRangeAddress);
                        rowNum = rowNum + v;
                    }
                    MemoryStream ms = new MemoryStream();
                    hssfworkbook.Write(ms);
                    HttpContext curContext = HttpContext.Current;
                    curContext.Response.ContentType = "application/vnd.ms-excel";
                    curContext.Response.ContentEncoding = Encoding.UTF8;
                    curContext.Response.Charset = "";
                    curContext.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode(pjName + DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8)));
                    curContext.Response.BinaryWrite(ms.ToArray());
                    // curContext.Response.End();
                    curContext.ApplicationInstance.CompleteRequest();
                    hssfworkbook = null;
                    ms.Close();
                    ms.Dispose();
                }
  • 相关阅读:
    vue Syntax Error: Unexpected token {
    MQ 分拆Json数据包然后上传
    京东商城投诉商家
    C# 读写Txt文件
    DB2时间函数 实现 时间加减
    VS恢复默认设置
    只用一次循环开销 将类似 1 A 、1 B 的数据返回成为 1 A,B 的拼接形式
    DB2 With语句递归
    属性与字段的区别
    With语句在数据统计应用
  • 原文地址:https://www.cnblogs.com/bijiang/p/5828083.html
Copyright © 2011-2022 走看看