【发布时间】:2016-02-15 16:58:00
【问题描述】:
我正在将数据从 DataGridView 导出到 Excel。一切正常,但是当列中的数据具有例如:000115455 时,Excel 会忽略“0”并插入 115455。我接下来尝试:
- 在填充DataGridView之前格式化DataTable中的列
- 将从数据库检索的数据转换为 VARCHAR。
- 在填充后格式化 DataGridView 中的列。
- 在我的方法中使用 Range.EntireColumn、Range.Cells.NumberFormat 等方式。
这些都不行!
下面是导出到excel并保存文件的一段代码
ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelLibro = ExcelApp.Workbooks.Add(misDatos);
ExcelHoja = (Excel.Worksheet)ExcelLibro.Worksheets.get_Item(1);
Excel.Range rangoHoja = (Excel.Range)ExcelHoja.Cells[1, 1];
rangoHoja.Select();
ExcelHoja.PasteSpecial(rangoHoja, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
ExcelLibro.SaveAs(opcionSFD.FileName,
Excel.XlFileFormat.xlWorkbookNormal,
misDatos,
misDatos,
misDatos,
misDatos,
Excel.XlSaveAsAccessMode.xlExclusive,
misDatos,
misDatos,
misDatos,
misDatos,
misDatos);
ExcelApp.DisplayAlerts = true;
ExcelLibro.Close(true, misDatos, misDatos);
ExcelApp.Quit();
我能做什么?
【问题讨论】:
标签: c# excel winforms datagridview