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

前文说道写入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 ? "工程师" : "经理";

                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();

可实现在网页中弹出提示框保存文件。
工程下载: http://download.csdn.net/detail/yysyangyangyangshan/9037569

你可能感兴趣的:(NPOI操作Excel 004:写入空Excel(增加保存提示框))