单元格样式未从代码隐藏中应用于Excel工作表

时间:2019-04-23 21:26:31

标签: c# asp.net excel npoi xssf

我有一个网页,正在尝试设计新工作簿的样式。我是在c#中使用NPOI.XSSF.UserModel的新手。我从网页创建了一个新的excel工作簿,并尝试了各种单元格的样式,在创建和导出文件时没有错误,但是当我打开excel文件时,会看到一个确认框

  

“我们发现'Template.xlsx中的某些内容存在问题。您是否希望我们尽可能地恢复...。”,

我单击“是”,唯一已应用的样式是“自动调整大小”。这是我的功能的摘录。

using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using ClosedXML.Excel;
using DocumentFormat.OpenXml.Office2010.ExcelAc;

protected void ExportToExcel(object sender, EventArgs e)
        {
            string columnName = "";
            string fileName = ViewState["KPIListName"].ToString();

            GlobalClass globalClass = new GlobalClass();

            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("Sheet1");
            XSSFRow row1 = (XSSFRow)sheet.CreateRow(0);

            ICellStyle cellStyle = workbook.CreateCellStyle();
            ICellStyle cellStyleLocked = workbook.CreateCellStyle();
            ICellStyle cellHeaderStyle = workbook.CreateCellStyle();

            var headerFont = workbook.CreateFont();

            headerFont.IsBold = true;
            cellHeaderStyle.SetFont(headerFont);

            cellStyle.FillBackgroundColor = IndexedColors.Grey25Percent.Index;
            cellStyleLocked.IsLocked = true;

            for (int j = 0; j < dt.Columns.Count; j++)
            {
                XSSFCell cell = (XSSFCell)row1.CreateCell(j);

                columnName = dt.Columns[j].ToString();
                cell.SetCellValue(columnName);
                cell.CellStyle = cellHeaderStyle;
                cell.CellStyle = cellStyle;
            }

            //loops through the data
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                XSSFRow row = (XSSFRow)sheet.CreateRow(i + 1);

                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    XSSFCell cell = (XSSFCell)row.CreateCell(j);

                    if (j == 0)
                    {
                        cell.SetCellValue(columnName);
                        cell.CellStyle = headerStyle;
                        cell.CellStyle = cellStyle;
                    }
                    //else
                    //{
                    //    cellStyle.FillBackgroundColor = IndexedColors.White.Index;
                    //    cellStyle.FillPattern = FillPattern.SolidForeground;
                    //}

                    columnName = dt.Columns[j].ToString();

                    cell.SetCellValue(dt.Rows[i][columnName].ToString());
                    cell.CellStyle = cellHeaderStyle;
                    cell.CellStyle = cellStyle;
                    cell.CellStyle = cellStyleLocked;

                    sheet.AutoSizeColumn(j);
                }
            }

            using (var exportData = new MemoryStream())
            {
                Response.Clear();
                workbook.Write(exportData);
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "Template.xlsx"));
                Response.BinaryWrite(exportData.ToArray());
            }

            Response.End();
        }

这是预期的结果,第一行必须为粗体,ItemID,“期间类型”和“期间持续时间”行必须隐藏,所有可见的单元格都必须锁定,以防止用户篡改该单元格。

KPI         ActiveDateAndTime   DMS.DMS Production.AMod
ItemID                      36E59B17-C14D-4C56-6209
Plan Type                   Budget
Period Type                 Hour
Period Duration                 8
UoM                     rd
ActiveDateAndTime   2019-04-01 07:00:00 AM  
ActiveDateAndTime   2019-04-01 03:00:00 PM  
ActiveDateAndTime   2019-04-01 11:00:00 PM  
ActiveDateAndTime   2019-04-02 07:00:00 AM  
ActiveDateAndTime   2019-04-02 03:00:00 PM  
ActiveDateAndTime   2019-04-02 11:00:00 PM  

任何帮助将不胜感激。

0 个答案:

没有答案