本文介绍了如何将日期值从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;

推荐答案


这篇关于如何将日期值从datatable传递给excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 10:36