基于初始格式格式化单元格[非常慢!]

时间:2017-04-30 16:01:17

标签: c# excel

我想创建一个使用Excel互操作dll将Excel文件导出为HTML文件的应用程序。一个问题是,我想确保.HTML文件预览看起来像在Excel中查看一样接近现实。

通常将Excel文件导出为HTML文件时,不会显示边框/“网格线”。要解决此问题,请you must add your own borders.

但是当Excel呈现coloured cells, grid lines are not shown.

我编写了下面的C#代码,为没有填充的单元格添加边框:

using Microsoft.Office.Interop.Excel;

namespace CS_HelloExcel
{
    class Program
    {
        static void Main()
        {
            //Create excel application:
            Application application = new Application();

            //Define filename "C:\Users\sancarn\Documents\"
            String filename = "C:\\Users\\sancarn\\Documents\\myFormatedXL.xlsx";

            //Open workbook as read only, don't update links
            Workbook workbook = application.Workbooks.Open(filename, false, true);

            foreach (Worksheet sheet in workbook.Sheets)
            {
                Range ur = sheet.UsedRange;
                ur = ur.Resize[ur.Rows.Count + 1, ur.Columns.Count + 1];

                //Create individual cells where blank cells originally occur - Takes 0.2s
                ur.Replace("", "'");

                // Takes 3.4s
                foreach(Range cell in ur.Cells)
                {
                    if(cell.Interior.Pattern == -4142)
                    {
                        Borders b = cell.Borders;
                        b[XlBordersIndex.xlEdgeBottom].LineStyle= XlLineStyle.xlContinuous;
                        b[XlBordersIndex.xlEdgeBottom].ThemeColor = 3;
                        b[XlBordersIndex.xlEdgeBottom].Weight = 2;
                        b[XlBordersIndex.xlEdgeLeft].LineStyle= XlLineStyle.xlContinuous;
                        b[XlBordersIndex.xlEdgeLeft].ThemeColor = 3;
                        b[XlBordersIndex.xlEdgeLeft].Weight = 2;
                        b[XlBordersIndex.xlEdgeRight].LineStyle= XlLineStyle.xlContinuous;
                        b[XlBordersIndex.xlEdgeRight].ThemeColor = 3;
                        b[XlBordersIndex.xlEdgeRight].Weight = 2;
                        b[XlBordersIndex.xlEdgeTop].LineStyle= XlLineStyle.xlContinuous;
                        b[XlBordersIndex.xlEdgeTop].ThemeColor = 3;
                        b[XlBordersIndex.xlEdgeTop].Weight = 2;
                    }
                }
            }

            //Save workbook as html file takes 0.5s
            application.DisplayAlerts = false;
            workbook.SaveAs("myFormatedXL.html",XlFileFormat.xlHtml);

            //Get workbook path
            String path = workbook.FullName;

            //Close application
            workbook.Close(false);
            application.Quit();

            //Free up memory
            application = null;
        }
    }
}

然而,如评论中所示,所有单元格的循环需要3.5秒......我想知道是否有人知道如何加快这项任务?

1 个答案:

答案 0 :(得分:0)

此选项似乎有更好的结果:

//Takes 0ms?
Range b=null;
foreach(Range cell in ur.Cells)
{
    if(cell.Interior.Pattern == -4142)
    {
        b = b!=null ? application.Union(b, cell) : cell;
    }
}

b.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
b.Borders[XlBordersIndex.xlEdgeBottom].ThemeColor = 3;
b.Borders[XlBordersIndex.xlEdgeBottom].Weight = 2;
b.Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
b.Borders[XlBordersIndex.xlEdgeTop].ThemeColor = 3;
b.Borders[XlBordersIndex.xlEdgeTop].Weight = 2;
b.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
b.Borders[XlBordersIndex.xlEdgeLeft].ThemeColor = 3;
b.Borders[XlBordersIndex.xlEdgeLeft].Weight = 2;
b.Borders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
b.Borders[XlBordersIndex.xlEdgeRight].ThemeColor = 3;
b.Borders[XlBordersIndex.xlEdgeRight].Weight = 2;
b.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = XlLineStyle.xlContinuous;
b.Borders[XlBordersIndex.xlInsideHorizontal].ThemeColor = 3;
b.Borders[XlBordersIndex.xlInsideHorizontal].Weight = 2;
b.Borders[XlBordersIndex.xlInsideVertical].LineStyle = XlLineStyle.xlContinuous;
b.Borders[XlBordersIndex.xlInsideVertical].ThemeColor = 3;
b.Borders[XlBordersIndex.xlInsideVertical].Weight = 2;

首先我们制作需要格式化的范围,然后我们应用格式。但是由于某种原因,由此产生的格式是......奇怪的(在生成的html文件中)。

我可能想在CSS中这样做。