如何将日期值从datatable传递给excel

编程入门 行业动态 更新时间:2024-10-26 16:25:11
本文介绍了如何将日期值从datatable传递给excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个字符串变量'v_DateForLabelGeneration',其中我存储日期。在数据表中,我有一个列'LblDate'。我能够正确地将此变量赋值给'LblDate'。然后我使用此数据表与其他列一起导出到excel文件。在excel文件中,我没有看到实际的日期,但有些像########这样的垃圾。以下是我的代码:

I have a string variable 'v_DateForLabelGeneration' wherein I store date. In datatable I have a column 'LblDate'. I am able to assign this variable to that 'LblDate' correctly. I then use this datatable for exporting to excel file alongwith other columns. In excel file I dont see the actual date but some junk like '########'. Hereunder is my code:

v_todaydate = (DateTime.Today); string v_DateForLabelGeneration = v_todaydate.AddDays(1).Date.ToString("yyyy/MM/dd");

dtMainSQLData1.Rows [ii - 1] [LblDate] = v_DateForLabelGeneration; //导出到excel DataColumnCollection dcCollection1 = dtMainSQLData1.Columns; Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp1 = new Microsoft.Office.Interop.Excel.ApplicationClass(); ExcelApp1.Application.Workbooks.Add(Type.Missing); for(int iii = 1; iii< dtMainSQLData1.Rows.Count + 1; iii ++) { for(int j = 1; j< dtMainSQLData1.Columns.Count + 1; j ++) { if(iii == 1) ExcelApp1.Cells [iii,j] = dcCollection1 [j - 1] .ToString(); ExcelApp1.Cells [iii + 1,j] = dtMainSQLData1.Rows [iii - 1] [j - 1] .ToString(); } } //ExcelApp.ActiveWorkbook.SaveCopyAs(\"C:\\Users\\RAJENDRAN \\Desktop\\guestaddress.xls\"); string filename1 =guestaddress+ DateTime.Now.ToString(ddMMyyyy)+。xls; ExcelApp1.ActiveWorkbook.SaveCopyAs(C:\\用户\\RAJENDRAN \\Desktop \\+ filename1); ExcelApp1.ActiveWorkbook.Saved = true;

dtMainSQLData1.Rows[ii - 1]["LblDate"] = v_DateForLabelGeneration; //export to excel DataColumnCollection dcCollection1 = dtMainSQLData1.Columns; Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp1 = new Microsoft.Office.Interop.Excel.ApplicationClass(); ExcelApp1.Application.Workbooks.Add(Type.Missing); for (int iii = 1; iii < dtMainSQLData1.Rows.Count + 1; iii++) { for (int j = 1; j < dtMainSQLData1.Columns.Count + 1; j++) { if (iii == 1) ExcelApp1.Cells[iii, j] = dcCollection1[j - 1].ToString(); ExcelApp1.Cells[iii + 1, j] = dtMainSQLData1.Rows[iii - 1][j - 1].ToString(); } } //ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\RAJENDRAN\\Desktop\\guestaddress.xls"); string filename1 = "guestaddress" + DateTime.Now.ToString("ddMMyyyy") + ".xls"; ExcelApp1.ActiveWorkbook.SaveCopyAs("C:\\Users\\RAJENDRAN\\Desktop\\" + filename1); ExcelApp1.ActiveWorkbook.Saved = true;

推荐答案

当您在Excel中看到########时,通常是因为列太窄而无法显示值,并且右侧有一列数据,因此它不能只是将其从细胞中流出到下一列。尝试使用在列标题上处理的调整大小使其更大,您可能会看到您的日期! When you see '########' in Excel, it is normally because the column is too narrow to show the value, and there is a column with data to the right, so it can't just "flow" it out of the cell into the next column. Try using the resize handled on teh column header to make it bigger and you may well see your date!

更多推荐

如何将日期值从datatable传递给excel

本文发布于:2023-07-22 13:38:02,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   日期   excel   datatable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!