如何将日期值从datatable传递给excel [英] How to pass a date value to excel from datatable

查看:95
本文介绍了如何将日期值从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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆