当我将值从数据表复制到Excel工作表时,我丢失了前导零。那是因为Excel可能会将值视为数字而不是文本。
我在C#中创建了工作表,我正在复制这样的值:
myWorksheet.Cells[i + 2, j] = dtCustomers.Rows[i][j - 1].ToString();
如何将整列或每个单元格格式化为文本?一个相关的问题,如何投射myWorksheet.Cells[i + 2, j]
以显示Intellisense中的样式属性?
答案 0 :(得分:38)
下面是一些代码,用于将列A和C格式化为SpreadsheetGear for .NET中的文本,其中的API与Excel类似 - 除了SpreadsheetGear经常更强类型的事实。要弄清楚如何将其转换为使用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
答案 1 :(得分:13)
如果将单元格格式设置为文本之前以添加前导零的数值,则会保留前导零,而不必通过添加撇号来扭曲结果。如果您尝试手动将前导零值添加到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();
}
}
答案 2 :(得分:6)
在您写入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
答案 3 :(得分:4)
我最近也和这个问题作斗争,我已经了解了上述建议的两个方面。
这种误导性的方面是你现在在单元格中有不同的值。幸运的是,当您复制/粘贴或导出为CSV时,不包括撇号。
结论:使用撇号,而不是数字格式以保留前导零。
答案 4 :(得分:2)
if (dtCustomers.Columns[j - 1].DataType != typeof(decimal) && dtCustomers.Columns[j - 1].DataType != typeof(int))
{
myWorksheet.Cells[i + 2, j].NumberFormat = "@";
}
答案 5 :(得分:2)
适用于Excel Interop的解决方案:
myWorksheet.Columns[j].NumberFormat = "@"; // column as a text
myWorksheet.Cells[i + 2, j].NumberFormat = "@"; // cell as a text
此代码应在 之前运行数据到Excel。列号和行号从1开始。
更多细节。虽然SpreadsheetGear参考的公认响应看起来几乎是正确的,但我有两个担忧:
答案 6 :(得分:1)
使用您的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 = "@";
答案 7 :(得分:0)
//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);
答案 8 :(得分:0)
我知道这个问题已经老了,我想贡献一点。
应用Range.NumberFormat = "@"
只能部分解决问题:
应用撇号表现得更好。它将格式设置为文本,将数据向左对齐,如果您使用 type 公式检查单元格中值的格式,它将返回 2含义文字