1. 程式人生 > >NPOI操作Excel 004:寫入空Excel(添加保存提示框)

NPOI操作Excel 004:寫入空Excel(添加保存提示框)

tail table add 內容 int null wro appdomain weight

前文說道寫入excel的樣例,當中保存Excle後須要添加提示框。讓用戶自己選擇保存路徑,做改動例如以下。


引用的dll等前面已經說過了,

直接看代碼:

        protected void Btn_WriteExcel(object sender, EventArgs e)
        {
            //要保存的內容,此處用代碼生成的內容。而在實際中能夠是數據庫讀取的,
            //亦或是頁面輸入的內容
            
            DataTable dt = new DataTable();

            dt.Columns.Add("序號");

            dt.Columns.Add("姓名");

            dt.Columns.Add("年齡");

            dt.Columns.Add("職位");

            for (int i = 0; i < 5; i++)
            {
                DataRow row = dt.NewRow();

                row["序號"] = i + 1;

                row["姓名"] = "Test"+i ;

                row["年齡"] = 25 + i;

                row["職位"] = i % 2 == 0 ? "project師" : "經理";

                dt.Rows.Add(row);
            }
            //為了更好的看怎樣使用NPOI。此處顯示兩行標題。
            //顯示標題能夠看怎樣合並單元格
            string mainTitle = "主標題";

            string secondTitle = "副標題";

            //保存的Excel路徑,文件名稱用guid生成
            string fileIndex = HttpRuntime.AppDomainAppPath.ToString();

            string tempExcel = fileIndex + @"\ExcelFile\{0}.xls";

            tempExcel = string.Format(tempExcel, System.Guid.NewGuid());

            int rowIndex = 0;
            
            //操作Excel的幾個主要對象,此處聲明
            HSSFWorkbook workbook = new HSSFWorkbook();

            HSSFSheet sheet = workbook.CreateSheet();

            //row0和row1是兩行標題
            HSSFRow row0 = sheet.CreateRow(rowIndex);

            HSSFCell cell0 = row0.CreateCell(0);

            cell0.SetCellValue(mainTitle);

            HSSFCellStyle style = workbook.CreateCellStyle();

            style.Alignment = CellHorizontalAlignment.CENTER;

            HSSFFont font = workbook.CreateFont();

            font.Boldweight = short.MaxValue;

            style.SetFont(font);

            cell0.CellStyle = style;

            //此處合並單元格
            sheet.AddMergedRegion(new NPOI.HSSF.Util.CellRangeAddress(rowIndex, rowIndex, 0, 5));

            rowIndex++;

            HSSFRow row1 = sheet.CreateRow(rowIndex);

            HSSFCell cell1 = row1.CreateCell(0);

            cell1.SetCellValue(secondTitle);

            cell1.CellStyle = style;

            sheet.AddMergedRegion(new NPOI.HSSF.Util.CellRangeAddress(rowIndex, rowIndex, 0, 5));

            //由於列名已經指定,占一行
            rowIndex++;

            //這一行顯示表頭
            HSSFRow row2 = sheet.CreateRow(rowIndex);

            int row2cellIndex = 0;

            foreach (DataColumn col in dt.Columns)
            {
                HSSFCell cell = row2.CreateCell(row2cellIndex);

                cell.SetCellValue(col.ColumnName.ToString());

                row2cellIndex++;
            }
            
            rowIndex++;

            //datatable的內容
            for(int i= 0;i< dt.Rows.Count;i++)
            {
                HSSFRow row = sheet.CreateRow(rowIndex);

                foreach (DataColumn col in dt.Columns)
                {
                    row.CreateCell(col.Ordinal).SetCellValue(dt.Rows[i][col].ToString());

                }

                rowIndex++;
            }

            //使用文件流保存
            MemoryStream ms = new MemoryStream();

            workbook.Write(ms);

            ms.Flush();

            ms.Position = 0;

            workbook = null;

            sheet = null;

            using (FileStream fs = new FileStream(tempExcel, FileMode.Create, FileAccess.ReadWrite))
            {
                ms.WriteTo(fs);
            }

            Response.Clear();
            Response.ClearHeaders();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachent;filename=" + HttpUtility.UrlDecode(@"TestExcel.xls"));//TestExcel.xls可改動
            Response.WriteFile(tempExcel, true);
            Response.Flush();
            Response.Close();

            if (File.Exists(tempExcel))
            {
                File.Delete(tempExcel);
            }

            Response.End();

            ms.Close();
        }
添加的是這段代碼:

            Response.Clear();
            Response.ClearHeaders();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachent;filename=" + HttpUtility.UrlDecode(@"TestExcel.xls"));//TestExcel.xls可改動
            Response.WriteFile(tempExcel, true);
            Response.Flush();
            Response.Close();

可實如今網頁中彈出提示框保存文件。
project下載:http://download.csdn.net/detail/yysyangyangyangshan/9037569

NPOI操作Excel 004:寫入空Excel(添加保存提示框)