zoukankan      html  css  js  c++  java
  • 导出到excel

            /// <summary>
            /// 导出
            /// </summary>
            /// <param name="table">数据表</param>
            /// <param name="SheetName">工作簿名称</param>
            /// <param name="reportName">报表名称</param>
            public static void Output(DataTable table, string SheetName, string reportName)
            {
                string result = string.Empty;
                try
                {
                    HSSFWorkbook workBook = new HSSFWorkbook();
                    Sheet sheet = workBook.CreateSheet(SheetName);  //sheet页名称
                    NPOI.SS.UserModel.Font font = workBook.CreateFont();
                    font.FontName = "微软雅黑";
    
                    font.FontHeight = 175;
                    CellStyle style = workBook.CreateCellStyle();
                    style.Alignment = HorizontalAlignment.CENTER;
                    style.VerticalAlignment = VerticalAlignment.CENTER;
                    style.SetFont(font);
                    style.WrapText = true;
    
                    Row rows = sheet.CreateRow(0);
                    rows.Height = 400;
                    rows.CreateCell(0).SetCellValue(reportName);   //报表名称
                    rows.GetCell(0).CellStyle = style;
                    sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1));
    
                    rows = sheet.CreateRow(1);
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        rows.CreateCell(i).SetCellValue(table.Columns[i].ColumnName.ToString());
                        rows.Sheet.SetColumnWidth(i, 4000);
                        rows.GetCell(i).CellStyle = style;
                    }
                    CellStyle style2 = workBook.CreateCellStyle();
                    style2.Alignment = HorizontalAlignment.LEFT;
                    style2.VerticalAlignment = VerticalAlignment.CENTER;
                    style2.SetFont(font);
                    style2.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
                    //style2.WrapText = true;
                    int tem = 0;
                    int sheetCount = 1;
                    for (int j = 1; j <= table.Rows.Count; j++)
                    {
                        tem++;
                        if (tem == 60000)//每页最多导出60000
                        {
                            tem = 0;
                            sheetCount++;
                            sheet = workBook.CreateSheet(SheetName + sheetCount);
                            rows = sheet.CreateRow(0);
                            for (int i = 0; i < table.Columns.Count; i++)
                            {
                                rows.CreateCell(i).SetCellValue(table.Columns[i].ColumnName.ToString());
                                rows.Sheet.SetColumnWidth(i, 4000);
                                rows.GetCell(i).CellStyle = style;
                            }
                        }
                        Row row = sheet.CreateRow(tem + 1);
                        for (int k = 0; k < table.Columns.Count; k++)
                        {
                            row.CreateCell(k).SetCellValue(table.Rows[j - 1][k].ToString());
                            row.GetCell(k).CellStyle = style2;
                        }
                        #region
                        //Row row = sheet.CreateRow(j + 1);
                        //for (int k = 0; k < table.Columns.Count; k++)
                        //{
                        //    row.CreateCell(k).SetCellValue(table.Rows[j - 1][k].ToString());
                        //    row.GetCell(k).CellStyle = style2;
                        //}
                        #endregion
                    }
    
                    int maxColumn = table.Columns.Count;
                    //列宽自适应,只对英文和数字有效  
                    for (int i = 0; i <= maxColumn; i++)
                    {
                        sheet.AutoSizeColumn(i);
                    }
    
                    using (MemoryStream ms = new MemoryStream())
                    {
                        workBook.Write(ms);
                        ms.Flush();
                        ms.Position = 0;
                        workBook = null;
    
                        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";//HttpContext.Current.Response
    
                        string browser = HttpContext.Current.Request.Browser.Browser.ToString();
                        string header = string.Empty;
                        if (browser == "Firefox")
                        {
                            header = string.Format("attachment; filename={0}", string.Format("{0}-{1}.xls", reportName, DateTime.Now.ToString("yyyy-MM-dd")), Encoding.UTF8).ToString();
                        }
                        else
                        {
                            header = string.Format("attachment; filename={0}", HttpUtility.UrlEncode(string.Format("{0}-{1}.xls", reportName, DateTime.Now.ToString("yyyy-MM-dd")), Encoding.UTF8)).ToString();
                        }
                        HttpContext.Current.Response.AddHeader("Content-Disposition", header);
                        HttpContext.Current.Response.Clear();
                        HttpContext.Current.Response.BinaryWrite(ms.GetBuffer());
                        HttpContext.Current.Response.End();
                    }
                }
                catch
                {
                    throw  new Exception("导出异常");
                }
            }
  • 相关阅读:
    程序中不要出现标识符完全相同的局部变量和全局变量
    不要出现仅靠大小写区分的相似的标识符
    程序中不要出现仅靠大小写区分的相似的标识符
    命名规则尽量与所采用的操作系统或开发工具的风格保持一致
    标识符的长度应当符合“min-length && max-information”原则
    标识符应当直观且可以拼读,可望文知意,不必进行“解码”
    当代码比较长,特别是有多重嵌套时,应当在一些段落的结束处加注释
    注释的位置应与被描述的代码相邻
    尽量避免在注释中使用缩写
    注释应当准确、易懂,防止注释有二义性
  • 原文地址:https://www.cnblogs.com/muxueyuan/p/4482048.html
Copyright © 2011-2022 走看看