1. 程式人生 > >NPOI導出

NPOI導出

alc lena tool tex horizon 客戶 header utf exce

技術分享圖片
//導出全部
            expertPara = GetExpetPara();
            expertPara.BeginIndex = pager.CurrentPageIndex;
            expertPara.EndIndex = int.MaxValue;

            DataSet ds = ExpertBLL.GetPriMngExpertDate(expertPara);
            DataTable dt = ds.Tables[0];
            //判斷加載哪個模板
            string tempath = "~/Templates/專家錄入管理表.xls";
            //1、獲取數據。
            using (FileStream file = new FileStream(HttpContext.Current.Server.MapPath(tempath), FileMode.Open, FileAccess.Read))
            {
                string ReportFileName = Server.MapPath("out.xls");
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
                HSSFSheet sheet1 = hssfworkbook.GetSheetAt(0) as HSSFSheet;
                sheet1.ForceFormulaRecalculation = true;
                sheet1.IsPrintGridlines = true;
                sheet1.DisplayGridlines = true;


                HSSFCellStyle _style = sheet1.Workbook.CreateCellStyle() as HSSFCellStyle;
                _style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                _style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
                int rowIndex = 0;

                {
                    rowIndex = 2;
                    int i = 1;
                    foreach (DataRow row in ds.Tables[0].Select())
                    {

                        int cellIndex = 0;
                        HSSFRow fr = GetRow(sheet1, rowIndex++);

                        GetCell(fr, cellIndex++, _style).SetCellValue(i.ToString());
                        GetCell(fr, cellIndex++, _style).SetCellValue(row["C_Name"].ToString());
                        GetCell(fr, cellIndex++, _style).SetCellValue(DelHTML(row["C_Sex"].ToString()));
                        GetCell(fr, cellIndex++, _style).SetCellValue(DelHTML(row["C_WorkUnit"].ToString()));
                        GetCell(fr, cellIndex++, _style).SetCellValue(row["C_expert_type"].ToString());
                        GetCell(fr, cellIndex++, _style).SetCellValue(row["C_position"].ToString());
                        GetCell(fr, cellIndex++, _style).SetCellValue(row["C_title"].ToString());
                        GetCell(fr, cellIndex++, _style).SetCellValue(row["C_Tel"].ToString());

                        GetCell(fr, cellIndex++, _style).SetCellValue(row["C_BusinessOutlets"].ToString());
                        GetCell(fr, cellIndex++, _style).SetCellValue(row["C_BankAccount"].ToString());
                 
                        i++;
                    }

                    sheet1.ForceFormulaRecalculation = true;

                    using (FileStream filess = File.OpenWrite(ReportFileName))
                    {
                        hssfworkbook.Write(filess);
                    }
                    System.IO.FileInfo filet = new System.IO.FileInfo(ReportFileName);
                    Response.Clear();
                    Response.Charset = "GB2312";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    // 添加頭信息,為"文件下載/另存為"對話框指定默認文件名   
                    string name = DateTime.Now + ".xls";
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
                    // 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進度   
                    Response.AddHeader("Content-Length", filet.Length.ToString());

                    // 指定返回的是一個不能被客戶端讀取的流,必須被下載   
                    Response.ContentType = "application/ms-excel";

                    // 把文件流發送到客戶端   
                    Response.WriteFile(filet.FullName);
                    // 停止頁面的執行   

                    Response.End();
                }
            }
技術分享圖片

NPOI導出