我需要一些數據導出到Excel工作表。數據從數據時間值和讀數中累積起來。 ,當我出口的日期時間值,他們改變自己的格式,以美國礦山問題的日期,而不是英國。C#Excel中的DateTime出口
我試圖通過添加格式到其中的日期時間值的範圍內對它進行排序,現在它甚至變得有趣。我注意到,直到9月份它正在處理英國格式的日期時間值,但在此之後它變爲美國版本。
打開excel文件我插入由應用單元的格式後,僅在美國的格式應用。
請看看我的代碼,它可能會給予幫助解決我的問題:
// Get dimensions of the 2-d array
int rowCount = data2.GetLength(0);
int columnCount = data2.GetLength(1);
// Get an Excel Range of the same dimensions
Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
range = range.get_Resize(rowCount, columnCount);
// Assign the 2-d array to the Excel Range
range.set_Value(Microsoft.Office.Interop.Excel.XlRangeValueDataType.xlRangeValueDefault, data2);
// Reset the range
range = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
range = range.get_Resize(rowCount, 1);
// Set the format type of the range
range.NumberFormat = "DD/MMM/YYYY hh:mm:ss";
編輯解決方案 隨着adrianm幫助這裏的解決方案代碼:
我離開之前包含的代碼相同,但我改變了創建data2對象數組的代碼(object [,] data2)
//create the empty array
var result = new object[data.Count, data[0].Count];
//insert all of the values in it
for (int i = 0; i < data.Count; i++)
{
for (int j = 0; j < data[i].Count; j++)
{
if (j < data[i].Count)
result[i, j] = data[i][j];
else
result[i, j] = " ";
}
}
//change the date time values to doubles which are
//in the first column after the first entry
for (int i = 1; i < data.Count; i++)
{
result[i, 0] = (Convert.ToDateTime(result[i, 0])).ToOADate();
}
return result;
在Excel中的日期只是數字。陣列中的日期更改爲與Excel的數量'DateTime.ToOADate()' – adrianm
@adrianm我的數組是對象,因爲它包含列標題等爲好。無論如何,我想我會嘗試從不同的數組中分離數據,會看到它做什麼然後 – Daniel
你不能只將日期轉換爲數組內的數字,像'data2 [x,y] = data2 [x ,y] .ToOADate()'? – adrianm