如何在excel中input数字前面的零?

我有一个用C#编写的应用程序,它从Datagridview中提取数据到excel。 我的问题是,当我有一个像010498这样的数字,当我输出到excel成为10498,没有零(0)在开始。 我想那里是零(0)。 有没有任何可能的解决scheme,要么在应用程序或通过Excel编写C#代码?
谢谢你,对不起的英语感到抱歉

在Datagridview中

导出为ex​​cel后

在第一张图片看来datagridview和第二张图片如何导出到excel后。 在单元格A3,F3,F4开始时缺less零(0)。 任何解决scheme我怎么能解决它? 谢谢。
导出到Excel的代码。

private void excel_toolstripmenuitem_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application excelApp = null; try { // instantiating the excel application class object misValue = System.Reflection.Missing.Value; excelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook currentWorkbook = excelApp.Workbooks.Add(Type.Missing); Microsoft.Office.Interop.Excel.Worksheet currentWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)currentWorkbook.ActiveSheet; currentWorksheet.Columns.ColumnWidth = 18; if (dg1.Rows.Count > 0) { currentWorksheet.Cells[1, 1] = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString(); int i = 1; foreach (DataGridViewColumn dgviewColumn in dg1.Columns) { // Excel work sheet indexing starts with 1 currentWorksheet.Cells[2, i] = dgviewColumn.Name; ++i; } Microsoft.Office.Interop.Excel.Range headerColumnRange = currentWorksheet.get_Range("A2", "AY2"); headerColumnRange.Font.Bold = true; headerColumnRange.Font.Color = 0xFF0000; int rowIndex = 0; for (rowIndex = 0; rowIndex < dg1.Rows.Count; rowIndex++) { DataGridViewRow dgRow = dg1.Rows[rowIndex]; for (int cellIndex = 0; cellIndex < dgRow.Cells.Count; cellIndex++) { currentWorksheet.Cells[rowIndex + 3, cellIndex + 1] = dgRow.Cells[cellIndex].Value; } } Microsoft.Office.Interop.Excel.Range fullTextRange = currentWorksheet.get_Range("A1", "AY" + (rowIndex + 1).ToString()); fullTextRange.WrapText = true; fullTextRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; } using (SaveFileDialog exportSaveFileDialog = new SaveFileDialog()) { exportSaveFileDialog.Title = "Save as"; exportSaveFileDialog.Filter = "Microsoft Office Excel Workbook(*.xls)|*.xls"; if (DialogResult.OK == exportSaveFileDialog.ShowDialog()) { string fullFileName = exportSaveFileDialog.FileName; currentWorkbook.SaveAs(fullFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, System.Reflection.Missing.Value, misValue, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution, true, misValue, misValue, misValue); currentWorkbook.Saved = true; MessageBox.Show("The export was successful", "Export to Excel", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (excelApp != null) { excelApp.Quit(); excelApp = null; } } } 

方法1

 currentWorksheet.Cells[rowIndex + 3, cellIndex + 1] = "'0" + dgRow.Cells[cellIndex].Value; 

方式2

 Case 1 

我相信你的datagrid单元格可以有不同长度的数字,例如0001234 。 所以,如果你想保留数据在数据网格中,那么不要设置像0000000这样的前缀格式。 使用方法1或单独格式化单元格。 我能想到的另一种方法是将DG单元的值存储在一个intvariables中,然后在考虑DG单元值的长度后使用.PadLeft 。 所以0000123这个数字的长度是7 。 然而,在一个intvariables中,它将被存储为123 (长度-3)。 所有你需要做的是使用.PadLeft7-3=4 zeros ,然后将结果string存储在Excel单元格中。

 Case 2 

如果你想所有的数字被存储为相同的长度与格式说00000000然后使用此代码。

 //~~> This will format Column 1 with "00000000" currentWorksheet.Columns[1].NumberFormat = "00000000"; 

PS:在Excel 2010 + VS 2010 Ultimate中testing

  currentWorksheet.Cells[...] = dgRow.Cells[cellIndex].Value; 

我猜这将是相关的代码行,你input的单元格中的数字。 分配NumberFormat属性强制Excel显示额外的前导0:

  currentWorksheet.Cells[rowIndex + 3, cellIndex + 1].NumberFormat = "000000"; 

假设你想要“010498”。

您可以在Excel中的数字之前加一个撇号,以保持前导零。

 '010498 

您仍然可以对带有前导'数字进行算术运算。

如果您知道数字的长度(例如,数字总是像您的示例一样是六位数字),那么您也可以使用全部为0的自定义数字格式(例如000000 )格式化单元格。

自定义数字格式

如果你正在使用一个DataTable,你需要拿另一个DataTable,然后你需要遍历数据表中的整个单元格,并用空格前面加上单元格文本,比如'&nbsp';我们不能修改同一个表格中的行,因为它会抛出一个exception说“收集已修改”。我们必须采取一个新的数据表。

考虑下面的代码。

  //To get the result with leading zero's in excel this code is written. DataTable dtUpdated=new DataTable(); //This gives similar schema to the new datatable dtUpdated = dtReports.Clone(); foreach (DataRow row in dtReports.Rows) { for (int i = 0; i < dtReports.Columns.Count; i++) { string oldVal = row[i].ToString(); string newVal = "&nbsp;"+oldVal; row[i] = newVal; } dtUpdated.ImportRow(row); } 

我们可以将这个更新的表绑定到datagrid,以便它对Excel转换很有用。