asp.net NPOI导出xlsx格式文件,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”

NPOI导出xlsx格式文件,会出现如下情况:

 

导出代码如下:

/// <summary>
        ///     将datatable数据写入excel并下载
        /// </summary>
        /// <param name="dt">datatable </param>
        /// <param name="excelName">文件名</param>
        /// <param name="templatePath">模板路径</param>
        /// <returns></returns>
        public static void DataTableToExcelAndDownload(DataTable dt, string excelName, string templatePath)
        {
            IWorkbook workbook = null;
            FileStream fs = null;
            IRow row = null;
            ISheet sheet = null;
            ICell cell = null;
            ICellStyle cellStyle = null;

            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    var rowCount = dt.Rows.Count; //行数
                    var columnCount = dt.Columns.Count; //列数

                    using (fs = File.OpenRead(templatePath))
                    {
                        //大批量数据导出的时候,需要注意这样的一个问题,Excel2003格式一个sheet只支持65536行,excel 2007 就比较多,是1048576
                        //workbook = new HSSFWorkbook(fs);//2003版本.xls
                        workbook = new XSSFWorkbook(fs); // 2007版本.xlsx
                    }

                    if (workbook != null)
                    {
                        sheet = workbook.GetSheetAt(0); //读取第一个sheet

                        //设置每行每列的单元格,
                        for (var i = 0; i < rowCount; i++)
                        {
                            row = sheet.CreateRow(i + 1);
                            for (var j = 0; j < columnCount; j++)
                            {
                                cell = row.CreateCell(j);
                                var value = dt.Rows[i][j];
                                var bdType = value.GetType().ToString();
                                switch (bdType)
                                {
                                    case "System.String":
                                        cell.SetCellValue(value.ToString());
                                        break;
                                    case "System.DateTime": //日期类型 
                                        cell.SetCellValue(
                                            Convert.ToDateTime(value.ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                                        break;
                                    case "System.Int16": //整型   
                                    case "System.Int32":
                                    case "System.Int64":
                                    case "System.Byte":
                                        var intV = 0;
                                        int.TryParse(value.ToString(), out intV);
                                        cell.SetCellValue(intV);
                                        break;
                                    case "System.Decimal": //浮点型   
                                    case "System.Double":
                                        double doubV = 0;
                                        double.TryParse(value.ToString(), out doubV);//格式化值
                                        cellStyle = workbook.CreateCellStyle();
                                        cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
                                        cell.SetCellValue(doubV);
                                        cell.CellStyle = cellStyle;                                        
                                        break;
                                    default:
                                        cell.SetCellValue(value.ToString());
                                        break;
                                }
                            }
                        }

                        var context = HttpContext.Current;
                        context.Response.Clear();
                        context.Response.ContentType = "application/vnd.ms-excel";
                        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + excelName);
                        using (var ms = new MemoryStream())
                        {
                            workbook.Write(ms);

                            long fileSize = ms.Length;
                            //加上设置大小下载下来的.xlsx文件打开时才不会报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
                            context.Response.AddHeader("Content-Length", fileSize.ToString());

                            context.Response.BinaryWrite(ms.GetBuffer());
                            context.ApplicationInstance.CompleteRequest();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                ExceptionHandling.ExceptionHandler.HandleException(ex);
            }
        }

加上设置大小下载下来的.xlsx文件打开时才不会报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”

long fileSize = ms.Length;
context.Response.AddHeader("Content-Length", fileSize.ToString());

即可。

posted @ 2017-10-25 15:43  一夜秋2014  Views(11988)  Comments(8Edit  收藏  举报