【问题标题】:C# Programatically change cell format of all Excel cells to GeneralC# 以编程方式将所有 Excel 单元格的单元格格式更改为常规
【发布时间】:2022-04-05 18:34:25
【问题描述】:

作为 ETL 过程的一部分,我将数据从各种不同的 Excel 文件导入数据库。在此之前,我需要能够将 Excel 工作表中所有单元格的单元格格式更改为“常规”格式。

我已经开始了,但我担心之后我不知道如何进步:

using Excel = Microsoft.Office.Interop.Excel;
.
.
.
String FilePath = "Code to get file location from database"
String SheetName = "Code to get SheetName from database"

Excel.Application MyApp = new Excel.Application();
MyApp.Visible = false;
Excel.Workbook myWorkbook = MyApp.Workbooks.Open(FilePath,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing);

//Code here to convert all rows to data type of general and then save

MyApp.Workbooks.Close();

对此的任何帮助将不胜感激

【问题讨论】:

    标签: c# excel interop


    【解决方案1】:

    你可以使用Range.NumberFormat属性:

    var myWorksheet = (Excel.Worksheet)myWorkbook.Worksheets[1];
    myWorksheet.Cells.NumberFormat = "General";
    

    请注意,如果您的工作表包含日期值,这可能会导致问题。

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2011-08-23
      相关资源
      最近更新 更多