在C#中将文本格式化为Excel列(或单元格)?

当我将数据表中的值复制到Excel工作表时,我正在丢失前导零。 这是因为Excel可能将数值视为数字而不是文本。

我在C#中创build了工作表,我正在复制这样的值:

myWorksheet.Cells[i + 2, j] = dtCustomers.Rows[i][j - 1].ToString(); 

如何将整列或每个单元格格式化为文本? 一个相关的问题,如何投射myWorksheet.Cells[i + 2, j]在Intellisense中显示样式属性?

下面是一些代码,将A和C列的格式设置为SpreadsheetGear for .NET中的文本,其中有一个类似于Excel的API – 除了SpreadsheetGear频繁更强types的事实。 不应该太难弄明白如何将其转换为Excel / COM的工作:

 IWorkbook workbook = Factory.GetWorkbook(); IRange cells = workbook.Worksheets[0].Cells; // Format column A as text. cells["A:A"].NumberFormat = "@"; // Set A2 to text with a leading '0'. cells["A2"].Value = "01234567890123456789"; // Format column C as text (SpreadsheetGear uses 0 based indexes - Excel uses 1 based indexes). cells[0, 2].EntireColumn.NumberFormat = "@"; // Set C3 to text with a leading '0'. cells[2, 2].Value = "01234567890123456789"; workbook.SaveAs(@"c:\tmp\TextFormat.xlsx", FileFormat.OpenXMLWorkbook); 

免责声明:我自己的SpreadsheetGear LLC

如果将单元格格式设置为“文本”, 然后添加前导零的数值,则前导零将保留,而不必通过添加撇号来扭曲结果。 如果您尝试手动向Excel中的默认工作表添加前导零值,然后将其转换为文本,则会删除前导零。 如果您先将单元格转换为文本,然后添加您的值,这很好。 以编程方式使用同样的原则。

  // Pull in all the cells of the worksheet Range cells = xlWorkBook.Worksheets[1].Cells; // set each cell's format to Text cells.NumberFormat = "@"; // reset horizontal alignment to the right cells.HorizontalAlignment = XlHAlign.xlHAlignRight; // now add values to the worksheet for (i = 0; i <= dataGridView1.RowCount - 1; i++) { for (j = 0; j <= dataGridView1.ColumnCount - 1; j++) { DataGridViewCell cell = dataGridView1[j, i]; xlWorkSheet.Cells[i + 1, j + 1] = cell.Value.ToString(); } } 

在写入Excel之前,需要更改格式:

 xlApp = New Excel.Application xlWorkSheet = xlWorkBook.Sheets("Sheet1") Dim cells As Excel.Range = xlWorkSheet.Cells 'set each cell's format to Text cells.NumberFormat = "@" 'reset horizontal alignment to the right cells.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight 

我最近也遇到了这个问题,我从上面的build议中学到了两件事。

  1. 将numberFormatting设置为@会导致Excel将该值左alignment,并将其读取为文本,但仍将截断前导零。
  2. 在开头添加撇号会导致Excel将其视为文本并保留为零,然后应用默认的文本格式,从而解决这两个问题。

这个误导的方面是,你现在在细胞中有不同的价值。 最后,当您复制/粘贴或导出为CSV时,不包括撇号。

结论:使用撇号,而不是numberFormatting为了保留前导零。

  if (dtCustomers.Columns[j - 1].DataType != typeof(decimal) && dtCustomers.Columns[j - 1].DataType != typeof(int)) { myWorksheet.Cells[i + 2, j].NumberFormat = "@"; } 

Excel Interop的解决scheme:

 myWorksheet.Columns[j].NumberFormat = "@"; // column as a text myWorksheet.Cells[i + 2, j].NumberFormat = "@"; // cell as a text 

这个代码应该把数据放到Excel 之前运行。 列和行号是基于1的。

多一点细节。 鉴于接受SpreadsheetGear的参考答案看起来几乎是正确的,我有两个担心:

  1. 我没有使用SpreadsheetGear。 我感兴趣的是没有任何第三方库的Excel interop通常的Excel通信,
  2. 我正在寻找按列编号的方式,而不是使用“A:A”这样的范围。

使用您的WorkSheet.Columns.NumberFormat ,并将其设置为string "@" ,这里是示例:

 Excel._Worksheet workSheet = (Excel._Worksheet)_Excel.Worksheets.Add(); //set columns format to text format workSheet.Columns.NumberFormat = "@"; 

注意:这个文本格式将适用于你的excel表格!

如果你想要一个特定的列应用文本格式,例如,第一列,你可以这样做:

 workSheet.Columns[0].NumberFormat = "@"; 

或者这会将指定范围的woorkSheet应用于文本格式:

 workSheet.get_Range("A1", "D1").NumberFormat = "@"; 
 //where [1] - column number which you want to make text ExcelWorksheet.Columns[1].NumberFormat = "@"; //If you want to format a particular column in all sheets in a workbook - use below code. Remove loop for single sheet along with slight changes. //path were excel file is kept string ResultsFilePath = @"C:\\Users\\krakhil\\Desktop\\TGUW EXCEL\\TEST"; Excel.Application ExcelApp = new Excel.Application(); Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(ResultsFilePath); ExcelApp.Visible = true; //Looping through all available sheets foreach (Excel.Worksheet ExcelWorksheet in ExcelWorkbook.Sheets) { //Selecting the worksheet where we want to perform action ExcelWorksheet.Select(Type.Missing); ExcelWorksheet.Columns[1].NumberFormat = "@"; } //saving excel file using Interop ExcelWorkbook.Save(); //closing file and releasing resources ExcelWorkbook.Close(Type.Missing, Type.Missing, Type.Missing); Marshal.FinalReleaseComObject(ExcelWorkbook); ExcelApp.Quit(); Marshal.FinalReleaseComObject(ExcelApp);