导出到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("导出异常");
            }
        }

 

posted on 2015-05-06 14:59  雪原日暮  阅读(171)  评论(0编辑  收藏  举报