【问题标题】:Formatting Excel Sheet using C#. The Style apply to all cells not to one cell使用 C# 格式化 Excel 工作表。样式适用于所有单元格而不是一个单元格
【发布时间】:2017-07-18 22:38:18
【问题描述】:

我正在使用扩展方法将 DataTable 导出到 Excel。当我格式化单元格样式、对齐方式和字体大小时,它适用于工作表中的所有单元格。

这是我的代码

public static void ExportToExcel(this System.Data.DataTable DataTable, string ExcelFilePath = null, string address = "", string reportName = "")
    {
        try
        {
            int ColumnsCount;

            if (DataTable == null || (ColumnsCount = DataTable.Columns.Count) == 0)
                throw new Exception("ExportToExcel: Null or empty input table!\n");

            // load excel, and create a new workbook
            Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
            Excel.Workbooks.Add();

            // single worksheet
            Microsoft.Office.Interop.Excel._Worksheet Worksheet = Excel.ActiveSheet;

            object[] Header = new object[ColumnsCount];

            // column headings               
            for (int i = 0; i < ColumnsCount; i++)
                Header[i] = DataTable.Columns[i].ColumnName;

            Microsoft.Office.Interop.Excel.Range HeaderRange = Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[10, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[10, ColumnsCount]));
            HeaderRange.Value = Header;
            HeaderRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gold);
            HeaderRange.Font.Bold = true;

            // DataCells
            int RowsCount = DataTable.Rows.Count;
            object[,] Cells = new object[RowsCount, ColumnsCount];

            for (int j = 0; j < RowsCount; j++)
                for (int i = 0; i < ColumnsCount; i++)
                    Cells[j, i] = DataTable.Rows[j][i];

            //Custom Header
            Worksheet.Range[Worksheet.Cells[1, 1], Worksheet.Cells[1, ColumnsCount]].Merge();
            Worksheet.Range[Worksheet.Cells[1, 1], Worksheet.Cells[2, ColumnsCount]].Merge();
            Worksheet.Cells[1, 1].Value = "DISTRIBUTOR SYSTEM";
            Worksheet.Cells[1, 1].Style.Font.Size = 18;
            Worksheet.Cells[1, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua);
            Worksheet.Cells[1].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            Worksheet.Cells[1].Style.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

            Worksheet.Range[Worksheet.Cells[3, 1], Worksheet.Cells[3, 2]].Merge();
            Worksheet.Cells[3, 1].Value = "Invoice Date";

            Worksheet.Cells[3, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua);
            Worksheet.Range[Worksheet.Cells[3, 3], Worksheet.Cells[3, 4]].Merge();
            Worksheet.Cells[3, 3].Value = System.DateTime.Today.ToShortDateString();
            Worksheet.Cells[3, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua);

            Worksheet.Range[Worksheet.Cells[4, 1], Worksheet.Cells[4, ColumnsCount]].Merge();
            Worksheet.Cells[4, 1].Value = reportName;
            Worksheet.Cells[4, 1].Style.Font.Size = 18;

            Worksheet.Range[Worksheet.Cells[6, 1], Worksheet.Cells[6, ColumnsCount]].Merge();
            Worksheet.Cells[6, 1].Value = address;

            //Worksheet.Range[Worksheet.Cells[1, 1], Worksheet.Cells[1, 7]].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            //Worksheet.Range[Worksheet.Cells[1, 1], Worksheet.Cells[1, 7]].Style.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

            //Worksheet.Range[Worksheet.Cells[11, 7], Worksheet.Cells[RowsCount + 1, 7]].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;

            Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[11, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[RowsCount + 1, ColumnsCount])).Value = Cells;
            Worksheet.Range[Worksheet.Cells[10, 4], Worksheet.Cells[RowsCount + 1, 4]].EntireColumn.NumberFormat = "yyyy/MM/dd hh:mm";

            Worksheet.Range[Worksheet.Cells[10, 11], Worksheet.Cells[RowsCount + 1, 7]].EntireColumn.NumberFormat = "####0.00";
            Worksheet.Cells[RowsCount + 2, 1].Value = "TOTAL";
            Worksheet.Cells[RowsCount + 2, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gold);
            Worksheet.Cells[RowsCount + 2, 7].Formula = "=Sum(" + Worksheet.Cells[11, 7].Address + ":" + Worksheet.Cells[RowsCount + 1, 7].Address + ")";

            Worksheet.Columns[7].ColumnWidth = 18.00;
            Worksheet.Columns[6].ColumnWidth = 18.00;
            Worksheet.Columns[5].ColumnWidth = 18.00;

            // check fielpath
            if (ExcelFilePath != null && ExcelFilePath != "")
            {
                try
                {
                    Worksheet.SaveAs(ExcelFilePath);
                    Excel.Quit();
                    MessageBox.Show("Excel file saved!");
                }
                catch (Exception ex)
                {
                    throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"
                        + ex.Message);
                }
            }
            else    // no filepath is given
            {
                Excel.Visible = true;
            }
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message);
            throw new Exception("ExportToExcel: \n" + ex.Message);
        }
    }

我只需要在单元格1、单元格4和单元格6中水平和垂直设置字体大小和对齐中心 请帮忙

【问题讨论】:

  • 您可以尝试在保存文件之前将格式化作为最后一步,看看是否有任何改变?
  • 有可能吗。请告诉我把代码放在哪里。我应该在检查文件路径之前把它放吗
  • 我把代码放在 Worksheet.SaveAs(ExcelFilePath);还是没有变化。 ☺
  • 让我们首先关注字体大小 - 尝试从您设置字体大小的所有行中删除 .Style。这里说使用样式可能会影响整个工作表:stackoverflow.com/questions/11740856/…
  • 是的。有效。感谢 Mihai Ovidiu Drăgoi。现在我将把这个改变应用到对齐上。再次感谢。

标签: c# excel


【解决方案1】:

在 Excel 互操作中使用 .Style 似乎会改变整个工作表的样式,如下所示:Changing font size of one cell in excel using C# 尝试将字体和对齐方式更改直接应用于不访问其样式的单元格/范围。

【讨论】:

    【解决方案2】:

    在单元格 1、单元格 4 和单元格 6 中水平和垂直设置字体大小和对齐中心...尝试

    {
        Worksheet.Cells[1, i].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
        Worksheet.Cells[1, i].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
        Worksheet.Cells[1, i].Style.Font.Size = 20;
    }
    

    【讨论】:

    • 这适用于OpenXml,而问题是关于Microsoft.Office.Interop.Excel。不同的包。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    相关资源
    最近更新 更多