日期的Excel互操作单元格格式 [英] Excel Interop cell formatting of Dates

查看:136
本文介绍了日期的Excel互操作单元格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序创建在Excel中输出



一些日期似乎越来越误解:



在打开该文件的日期是混合物中的截图。< BR>
。如果我居然把光标在Excel中的框计算(以screenprint),然后按输入单元格的格式恢复到正确的格式。



我M使用的Microsoft.Office.Interop.Excel 将数据从数据表移动到 Excel中模板保存在我的解决方案。



把数据进入细胞之前,我改变NumberFormat的相应使用每个列的切换

 的for(int i = 1; I<为numColumns + 1;我++){ 
开关(dt.Columns [I-1] .DataType.ToString()){
案System.Int32:
xlWorkSheet.Columns [Ⅰ] .NumberFormat =#,# #0_; [红色] - #,## 0;
中断;
案System.String:
xlWorkSheet.Columns [I] .NumberFormat =@;
中断;
案的System.DateTime:
xlWorkSheet.Columns [I] .NumberFormat =[$ -809] DD MMMM YYYY; @; //DD-MMM-YY
中断;
案System.Date:
xlWorkSheet.Columns [I] .NumberFormat =[$ -809] DD MMMM YYYY; @; //DD-MMM-YY
中断;
默认:
xlWorkSheet.Columns [I] .NumberFormat =@;
中断;
}
}

要从我使用嵌套循环DataTable中移动值

  //举动头总计到电子表格
的for(int i = 1; I<为numColumns + 1 ;我++){$​​ b $ b xlWorkSheet.Cells [1,I]。价值= dt.Columns [我 - 1] .ColumnName;
}

//数据
DataView的DV =新的数据视图(DT)移动;
dv.Sort =日期ASC;

INT rowCount等= 2;
串theValue;
尝试{
的foreach(在DV DataRowView的DR){//(DataRow的博士indt.Rows)
的for(int i = 1; I<为numColumns + 1;我++){$​​ B $ b theValue =博士[I - 1]的ToString();
xlWorkSheet.Cells [rowCount时,我]。价值= theValue;
}
rowCount时+ = 1;
}
}赶上(例外){
扔;
}

在存储过程中,当我填充的DataTable我试着明确地拼写出使用 DATETIME 的类型和以下内容:

  SELECT 
日期= CONVERT(DATE,b。[日期])
...

我怎样才能让我的日期数据,从而明确了Excel不能曲解,并应用了所有所需的格式?






修改



接下来在嵌套循环(未经测试)尝试如下:

  INT rowCount等= 2; 
串theValue;
的DateTime DTM;
的DateTime D组;
尝试{
的foreach(在DV DataRowView的DR){//(DataRow的博士indt.Rows)
的for(int i = 1; I<为numColumns + 1;我++){$​​ B $ b // theValue =博士[I - 1]的ToString();
//xlWorkSheet.Cells[rowCount,我]。价值= theValue;
开关(DR [我 - 1] .GetType()的ToString()){
案的System.DateTime:
DTM = Convert.ToDateTime(DR [我 - 1]) ;
xlWorkSheet.Cells [rowCount时,我]。价值= dtm.ToOADate();
中断;
案System.Date:
D = Convert.ToDateTime(DR [我 - 1]);
xlWorkSheet.Cells [rowCount时,我]。价值= d.ToOADate();
中断;
默认:
theValue =博士[I - 1]的ToString();
xlWorkSheet.Cells [rowCount时,我]。价值= theValue;
中断;
}

}
rowCount时+ = 1;
}
}赶上(例外){
扔;
}


解决方案

这里的问题是,你是到Excel作为一个字符串写入一切,Excel将其存储这样。在编辑并按下Enter键,Excel将重新诠释单元格的内容,然后可以存储不同的看法。



当你调用的ToString()的DateTime ,默认格式的格式为DD / MM / YY HH:MM:SS,而这正是你所得到的所看到的在Excel文件。



日期存储为代表自1900年以来已过去为了得到这个数的天数,你可以调用号码ToOADate()方法。



请参阅的 http://msdn.microsoft.com/en-gb/library/system.datetime.tooadate.aspx



通过数据设置为双击和适当的格式字符串,你应该得到你希望的结果!


My program creates an output in Excel.

Some of the dates seem to be getting misinterpreted:

On opening the file the dates are a mixture as in the screenshot.
If I actually put the cursor in Excel's calculation box (in screenprint) and press enter the formatting of the cell reverts to the correct formatting.

I'm using Microsoft.Office.Interop.Excel to move data from a Datatable to an Excel template saved in my Solution.

Before putting the data into the cells I change each columns numberformat accordingly using a switch:

for(int i = 1; i < NumColumns + 1; i++) {
    switch(dt.Columns[i-1].DataType.ToString()) {
        case "System.Int32":
            xlWorkSheet.Columns[i].NumberFormat = "#,##0_ ;[Red]-#,##0 ";
            break;
        case "System.String":
            xlWorkSheet.Columns[i].NumberFormat = "@";
            break;
        case "System.DateTime":
            xlWorkSheet.Columns[i].NumberFormat = "[$-809]dd mmmm yyyy;@"; //"dd-mmm-yy";
            break;
        case "System.Date":
            xlWorkSheet.Columns[i].NumberFormat = "[$-809]dd mmmm yyyy;@"; //"dd-mmm-yy";
            break;
        default:
            xlWorkSheet.Columns[i].NumberFormat = "@";
            break;
    }
}

To move the values from the datatable I use nested loops:

//move header totals into the spreadsheet
for(int i = 1; i < NumColumns + 1; i++) {
    xlWorkSheet.Cells[1, i].value = dt.Columns[i - 1].ColumnName;
}

//move in the data
DataView dv = new DataView(dt);
dv.Sort = "Date ASC";

int rowCount = 2;
string theValue;
try {
    foreach(DataRowView dr in dv) {//(DataRow dr indt.Rows)
        for(int i = 1; i < NumColumns + 1; i++) {
            theValue = dr[i - 1].ToString();
            xlWorkSheet.Cells[rowCount, i].value = theValue;
        }
        rowCount += 1;
    }
} catch(Exception) {
    throw;
}

In the stored procedure when I populate the Datatable I've tried explicitly spelling out the type using DATETIME and the following:

SELECT 
"Date" = CONVERT(DATE,b.[Date])
    ...

How can I make my Date data so explicit that Excel cannot misinterpret and all the desired formatting is applied ?


Edit

Next (untested) attempt at the nested loop is as follows:

int rowCount = 2;
string theValue;
DateTime dtm;
DateTime d;
try {
    foreach(DataRowView dr in dv) {//(DataRow dr indt.Rows)
        for(int i = 1; i < NumColumns + 1; i++) {
            //theValue = dr[i - 1].ToString();
            //xlWorkSheet.Cells[rowCount, i].value = theValue;
            switch(dr[i - 1].GetType().ToString()) {
                case "System.DateTime":
                    dtm = Convert.ToDateTime(dr[i - 1]);
                    xlWorkSheet.Cells[rowCount, i].value = dtm.ToOADate();
                    break;
                case "System.Date":
                    d = Convert.ToDateTime(dr[i - 1]);
                    xlWorkSheet.Cells[rowCount, i].value = d.ToOADate();
                    break;
                default:
                    theValue = dr[i - 1].ToString();
                    xlWorkSheet.Cells[rowCount, i].value = theValue;
                    break;
            }

        }
        rowCount += 1;
    }
} catch(Exception) {
    throw;
}

解决方案

The issue here is that you are writing everything to Excel as a string, and Excel will store it as such. On editing and pressing Enter, Excel will re-interpret the cell contents and may then store it differently.

When you call ToString() on a DateTime, the default formatting is in the form DD/MM/YY HH:mm:ss, which is exactly what you are seeing in the resultant file.

Dates in Excel are stored as numbers representing the number of days that have elapsed since 1900. To get this number, you can call the ToOADate() method.

See http://msdn.microsoft.com/en-gb/library/system.datetime.tooadate.aspx

With the data set as a doubleand an appropriate format string, you should get the result you're hoping for!

这篇关于日期的Excel互操作单元格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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