【问题标题】:export datetime to excel将日期时间导出到excel
【发布时间】:2015-02-19 12:19:46
【问题描述】:

我想将数据导出到 Windows 窗体应用程序中的 excel 文件,但是当我尝试导出日期时,格式发生了变化。我想使用 dd-MM-yyyy 格式

            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

        if (xlApp == null)
        {
            MessageBox.Show("Excel is not properly installed!!");
            return;
        }


        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);



        xlWorkSheet.Cells[1, 1].NumberFormat = "dd/mm/yyyy";
        xlWorkSheet.Cells[1, 1] = "19-02-2015";
        xlWorkSheet.Cells[2, 1] = "02-03-2015";
        xlWorkSheet.Cells[2, 1].NumberFormat = "dd/mm/yyyy";

        xlWorkBook.SaveAs("C:\\Users\\user\\Desktop\\Data.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);
    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

excel文件中的结果是: 19-2-2015 作为文本 02/03/2015 作为日期时间 我使用 excel 2013

那么如何将日期大于 12 的日期导出到 excel 并给它们格式 mm/dd/jjjj

【问题讨论】:

  • 它在第一个日期没有工作,输出仍然相同,第二个更改为 2/3/yyyy 但它后面的值是 3-2-2015 所以它切换了正确的日期和月份但将年份更改为 yyyy 编辑:将格式更改为 mm/dd/jjjj 现在第二个日期正在工作,但第一个日期仍然没有
  • 可能是因为 19 不是有效月份。 “mm/dd/jjjj”=>“19-02-2015”。在测试时,使用“mmm”格式来验证你得到了你想要的月份。您确定您的“02/03/2015”是 3 月 2 日而不是 2 月 3 日吗?你为什么不把你想要的日期放在电子表格中,然后打开你的应用程序并读取该文件,看看它应该如何存储? “19-02-2015”是否对应您当地的区域配置(dd-mm-yyyy)?
  • 嗨,你能试试这些行,如果能解决,请告诉我。我将其添加为答案 xlWorkSheet.Cells[1, 1].NumberFormat = "dd-mm-yyyy"; xlWorkSheet.Cells[2, 1].NumberFormat = "dd-mm-yyyy"; xlWorkSheet.Cells[1, 1].Value = "19-02-2015"; xlWorkSheet.Cells[2, 1].Value = "02-03-2015";
  • @Ewout 上面的 sn-p 对你有用吗?
  • @Andrew 我的本地配置是 dd-mm-yyyy,当我将月份更改为 ddd 时,它显示 2 月 3 日,所以你能给我一些电子表格 kishoreVM 的示例代码,格式为 dd-mm-yyyy不起作用,当它仍然具有值 2015 时,将导致年份显示为 yyyy

标签: c# date datetime export-to-excel


【解决方案1】:

我建议您使用“文本”作为单元格类型。它将摆脱日期的自动格式更改。您可以使用以下代码:

xlWorkSheet.Cells[1, 1].NumberFormat = "@";

【讨论】:

  • Dident 工作,第二个日期的结果是 42038,第一个日期发生了变化
  • 您需要在不更改格式的情况下对要放置日期的每个单元格使用此代码。或者您可以将文本设置为电子表格中所有单元格的列类型: Range cells = xlWorkSheet.Cells; cells.NumberFormat = "@";
  • 还是一样的结果
  • 请在以下行之前添加我上面评论中的代码 - xlWorkSheet.Cells[1, 1] = "19-02-2015";我相信它应该有效
  • 这两行 Range cells = xlWorkSheet.Cells; cells.NumberFormat = "@";应该在您的代码 xlWorkSheet.Cells[1, 1] = "19-02-2015"; 的以下行之前
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 2014-05-16
  • 2021-01-25
  • 1970-01-01
  • 2015-08-05
  • 2016-04-24
  • 2015-07-19
相关资源
最近更新 更多