我目前正在尝试将数据库中的某些数据导出到Excel文件中。除了一栏,一切都很顺利。
我的目标表(开始日期和结束日期)中有两个日期字段,我想在Excel表格中显示。问题是,结束日期正确显示(dd / mm / yyyy),而我的开始日期不是(mm / dd / yyyy)。他们的定义在数据库级别上完全相同。
以下是我如何构建我的DataTable:
public System.Data.DataTable ExportLastChangesToExcel()
{
List<HD_DISCOUNTS> listDiscount = (from d in dbHosp.HD_DISCOUNTS where d.TO_EXTRACT == "N" orderby d.EXTRACT_TEXT descending select d).ToList();
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("Condition Type", typeof(string));
table.Columns.Add("Sales organisation", typeof(string));
table.Columns.Add("Distribution Channel", typeof(string));
table.Columns.Add("Customer", typeof(string));
table.Columns.Add("Customer Name", typeof(string));
table.Columns.Add("Material", typeof(string));
table.Columns.Add("Material name", typeof(string));
table.Columns.Add("Amount", typeof(string));
table.Columns.Add("Unit", typeof(string));
table.Columns.Add("C..", typeof(string));
table.Columns.Add("Valid From", typeof(string));
table.Columns.Add("Valid To", typeof(string));
table.Columns.Add("Valid Action", typeof(string));
var allCust = (from v in dbCust.CUSTOMER_MASTER where v.INCLUDE_INTO_HD == "Y" select v).ToList();
foreach (HD_DISCOUNTS disc in listDiscount)
{
string compareId = disc.ID_HOSP.ToString();
CUSTOMER_MASTER cust = (from h in allCust where h.SOLDTOPARTY.TrimStart('0') == compareId select h).FirstOrDefault();
DIM_PRODUCT prod = (from p in db.DIM_PRODUCT where p.P_KEY_AZ == disc.ID_PRODUCT select p).FirstOrDefault();
DIM_PRODUCT_SAP sap = (from s in db.DIM_PRODUCT_SAP where s.P_KEY_AZ == prod.P_KEY_AZ && s.P_TYPE_SAP == "Domestic" select s).FirstOrDefault();
table.Rows.Add("ZD22", "BE10", "1", cust.SOLDTOPARTY, cust.SOLDTOPARTY_DESC, sap.P_KEY_SAP.ToString(), prod.P_DESC_AZ, disc.DISCOUNT.ToString(), "%", "A", disc.START_DATE.Value.ToString("dd/MM/yyyy"), disc.END_DATE.Value.ToString("dd/MM/yyyy"), disc.EXTRACT_TEXT);
}
return table;
}
我已调试并且应该很好地显示开始日期。这是我用来构建Excel的代码:
protected void btnExport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excel;
Microsoft.Office.Interop.Excel.Workbook worKbooK;
Microsoft.Office.Interop.Excel.Worksheet worksheet;
Microsoft.Office.Interop.Excel.Range celLrangE;
try
{
excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
excel.DisplayAlerts = false;
worKbooK = excel.Workbooks.Add(Type.Missing);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)worKbooK.ActiveSheet;
worksheet.Name = "Sheet1";
System.Data.DataTable tab = ExportLastChangesToExcel();
Range excelRange = worksheet.get_Range("K1");
excelRange.NumberFormat = "dd/mm/yyyy;@";
int rowcount = 2;
foreach (DataRow datarow in tab.Rows)
{
rowcount += 1;
for (int i = 1; i <= tab.Columns.Count; i++)
{
if (rowcount == 3)
{
worksheet.Cells[1, i] = tab.Columns[i - 1].ColumnName;
worksheet.Cells.Font.Color = System.Drawing.Color.Black;
}
worksheet.Cells[rowcount-1, i] = datarow[i - 1].ToString();
//worksheet.Cells[i, 11].NumberFormat = "@";
if (rowcount > 3)
{
if (i == tab.Columns.Count)
{
if (rowcount % 2 == 0)
{
celLrangE = worksheet.Range[worksheet.Cells[rowcount, 1], worksheet.Cells[rowcount, tab.Columns.Count]];
}
}
}
}
}
celLrangE = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[rowcount, tab.Columns.Count]];
celLrangE.EntireColumn.AutoFit();
celLrangE = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[2, tab.Columns.Count]];
worKbooK.SaveAs("C:\\Temp\\Hospital Discount\\Genpact\\GenpactExtract.xlsx");
worKbooK.Close();
excel.Quit();
}
catch (Exception ex)
{
}
finally
{
worksheet = null;
celLrangE = null;
worKbooK = null;
lblResultMessage.Text = "Extract for Genpact has been successfully generated";
lblResultMessage.Visible = true;
}
由于列K是包含错误日期的列,我尝试格式化此列以使我的日期格式正确,但没有成功。
对于可能出现的问题有什么想法?
答案 0 :(得分:0)
我认为问题是你要为Excel留下太多东西,需要明确你输入和呈现数据的方式。
我的建议:
示例:
Excel.Range r = worksheet.Cells[rowcount - 1, i];
if (datarow[i - 1] is DateTime)
{
r.Value2 = ((DateTime)(datarow[i - 1])).ToOADate();
r.NumberFormat = "dd/MM/yyyy";
}
else
{
r.Value2 = datarow[i - 1]
}
请记住,您可以一起跳过数据表,只需使用OracleDataReader
读取数据,然后将其直接放入Excel中。
此外,如果您有ODBC连接,您可以让Excel直接从Oracle读取它并将其放入表中 - 如果您这样做,它甚至可以正确处理日期。顺便说一句,您仍然可以使用几行代码从C#执行此操作,并让Excel执行繁重的工作。