我正在动态创建一个 excel 表并将值插入到其中。但是某些单元格中的值以错误的格式插入。以下是我在所选单元格中的 Excel 工作表 值应该是0002-9343但它被插入为Feb-43。这是由于 Excel 工作表中该列(D 列)的格式错误。在输入数据之前,我需要将整个 D 列(标题 D“data2”下的所有单元格)更改为“文本格式”。以下是创建excel表和插入数据的代码
excel = new Application();
excel.Visible = false;
wb = (_Workbook)(excel.Workbooks.Add(System.Reflection.Missing.Value));
sheet = wb.Sheets.Add();
sheet.Name = "TestSheet1";
sheet.Cells[1, "A"].Value2 = "Id";
sheet.Cells[1, "B"].Value2 = "Name";
sheet.Cells[1, "C"].Value2 = "Data1";
sheet.Cells[1, "D"].Value2 = "Data2";
sheet.Cells[1, "E"].Value2 = "Data3";
for (int i = 0; i < 10; i++)
{
id = i;
result = object;
data = JsonConvert.DeserializeObject(result);
name = (data != null) ? data.name : string.Empty;
data1 = (data != null) ? data.data1 : string.Empty;
data2 = (data != null) ? data.data2 : string.Empty;
data3 = (data != null) ? data.data3 : string.Empty;
sheet.Cells[i + 2, "A"].Value2 = name;
sheet.Cells[i + 2, "B"].Value2 = data1;
sheet.Cells[i + 2, "C"].Value2 = data2;
sheet.Cells[i + 2, "D"].Value2 = data3;
}
string ExcelPath = Some_path;
wb.SaveAsExcelPath,XlFileFormat.xlWorkbookNormal, null, null, false, false, XlSaveAsAccessMode.xlShared, false, false, null, null, null);
wb.Close(true);
excel.Quit();
现在,在循环之前,我需要将 Column-D 下的单元格格式更改为文本格式。如何从 c# 中的代码执行相同的操作?