【问题标题】:How to download excel file with styles on it?如何下载带有样式的excel文件?
【发布时间】:2019-07-26 05:14:36
【问题描述】:

我正在尝试将样式添加到 Excel 文件,然后下载它,该文件与数据一起下载,但样式不存在。有人可以帮我解决我失踪的任何事情吗?

            IApplication application = excelEngine.Excel;
            application.DefaultVersion = ExcelVersion.Excel2016;
            IWorkbook workbook = application.Workbooks.Create(1);
            IWorksheet namedSheet = workbook.Worksheets[0];
            namedSheet.Range["A1"].Text = "CustomerID";       
            namedSheet.Range["B1"].Text = "CompanyName";
            IStyle bodyStyle = workbook.Styles.Add("BodyStyle");
            bodyStyle.BeginUpdate();
            bodyStyle.Color = System.Drawing.Color.FromArgb(15, 19, 22);
            bodyStyle.EndUpdate();
            IStyle headerStyle = workbook.Styles.Add("HeaderStyle");
            headerStyle.BeginUpdate();
            headerStyle.Color = System.Drawing.Color.FromArgb(255, 174, 33);
            headerStyle.EndUpdate();
            namedSheet.Range["A10"].CellStyleName = "BodyStyle";
            namedSheet.Range["A1"].CellStyleName = "HeaderStyle";
            HttpContext.Response.ContentType = "application/vnd.ms-excel";
            MemoryStream ms = new MemoryStream();
            string separator = ",";                      //delimeter
            workbook.SaveAs(ms, separator);
            ms.Flush();
            byte[] bytes = ms.ToArray();
            ms.Close();
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=foo.xls");
            Response.AddHeader("Content-Type", "application/vnd.ms-excel");        Response.ContentType = "application/octet-stream";
            Response.BinaryWrite(bytes);
            Response.Write(Response.Output);
        }

下载的文件中只显示excel数据,不显示样式。

【问题讨论】:

    标签: c# asp.net-mvc syncfusion


    【解决方案1】:

    样式未保存在文件中的原因是您没有使用正确的 SyncFusion API。 您需要使用 style.BeginUpdate()style.EndUpdate() 方法来保持样式!

    例如:

        IStyle style = workbook.Styles.Add("style1");
        style.BeginUpdate();
        style.FillPattern = ExcelPattern.Gradient;
        style.Interior.Gradient.GradientStyle = ExcelGradientStyle.Vertical;
        style.Interior.Gradient.BackColor = Color.Black;
        style.Interior.Gradient.ForeColor = Color.Beige;
        //Apply style to range of cells
        namedSheet.Range["A10"].CellStyleName = "style1";
        style.EndUpdate();
        workbook.SaveAs(@"c:\temp\test.xlsx");
    
        workbook.Close();
        excelEngine.Dispose();
    

    【讨论】:

    • 我使用过这种格式,它太长了,所以我无法发布所有代码,当我使用 workbook.Saveas(@"c:\temp\test.xlsx" 保存文件时);样式在保存到我的计算机的文件中。但是在下载的文件上,它只是数据。我认为只有在我编写下载文件的代码的方式上存在错误
    • 请看我的代码 sn-p,您应该将以下 2 行放在各自的 BeginUpdate 和 EndUpdate 块中。 namedSheet.Range["A10"].CellStyleName = "BodyStyle"; namedSheet.Range["A1"].CellStyleName = "HeaderStyle";
    • 试过了,还是没有样式。
    【解决方案2】:
    using GemBox.Spreadsheet;
    
    .....
    
     worksheet.Cells[row += x, y].Value = ".Style.Font.Color =";
     worksheet.Cells[row, x].Value = "Color.Blue";
     worksheet.Cells[row, x].Style.Font.Color = SpreadsheetColor.FromName(ColorName.Blue);
    
     worksheet.Cells[row += x, y].Value = ".Style.Font.Italic =";
     worksheet.Cells[row, x].Value = "true";
     worksheet.Cells[row, x].Style.Font.Italic = true;
    
     worksheet.Cells[row += x, y].Value = ".Style.Font.Name =";
     worksheet.Cells[row, x].Value = "Comic Sans MS";
     worksheet.Cells[row, x].Style.Font.Name = "Comic Sans MS";
    
     worksheet.Cells[row += x, y].Value = ".Style.Font.Size =";
     worksheet.Cells[row, x].Value = "18 * 20";
     worksheet.Cells[row, x].Style.Font.Size = 18 * 20;
    

    【讨论】:

      【解决方案3】:

      CSV 是由逗号 (,) 分隔的纯文本,并且没有附加任何样式。当您将任何样式应用于单元格并将文件另存为 CSV 时,不会保留样式。这是 Microsoft Excel 的行为。我们已修改您的代码以生成保存应用样式的 xlsx 文件。请在下面找到更新后的代码。

      using (ExcelEngine excelEngine = new ExcelEngine())
                  {
                      IApplication application = excelEngine.Excel;
                      application.DefaultVersion = ExcelVersion.Excel2016;
                      IWorkbook workbook = application.Workbooks.Create(1);
                      IWorksheet namedSheet = workbook.Worksheets[0];
                      namedSheet.Range["A1"].Text = "CustomerID";
                      namedSheet.Range["B1"].Text = "CompanyName";
                      IStyle bodyStyle = workbook.Styles.Add("BodyStyle");
                      bodyStyle.BeginUpdate();
                      bodyStyle.Color = System.Drawing.Color.FromArgb(15, 19, 22);
                      bodyStyle.EndUpdate();
                      IStyle headerStyle = workbook.Styles.Add("HeaderStyle");
                      headerStyle.BeginUpdate();
                      headerStyle.Color = System.Drawing.Color.FromArgb(255, 174, 33);
                      headerStyle.EndUpdate();
                      namedSheet.Range["A10"].CellStyleName = "BodyStyle";
                      namedSheet.Range["A1"].CellStyleName = "HeaderStyle";
      
                      MemoryStream ms = new MemoryStream();
                      workbook.SaveAs(ms);
                      ms.Flush();
                      byte[] bytes = ms.ToArray();
                      ms.Close();
                      Response.Clear();
                      Response.AddHeader("Content-Disposition", "attachment; filename=foo.xlsx");
                      Response.AddHeader("Content-Length", bytes.Length.ToString());
                      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                      Response.BinaryWrite(bytes);
                  }
      

      我们还分享了示例供您参考,可以从以下链接下载。

      示例链接:https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample-1943806173.zip

      有关在 MVC 平台创建 Excel 文件的更多信息,请参阅以下文档。

      文档链接:https://help.syncfusion.com/file-formats/xlsio/create-read-edit-excel-files-in-asp-net-mvc-c-sharp#create-a-simple-excel-report

      问候,

      阿比拉米

      【讨论】:

        猜你喜欢
        • 2022-01-07
        • 1970-01-01
        • 2019-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-31
        • 1970-01-01
        相关资源
        最近更新 更多