我的问题是, 我有mysql数据库,当我插入数据时:
string date = dateTimePicker1.Value.ToString(“yyyy:MM:dd”);
MySqlCommand cmd = new MySqlCommand ("INSERT INTO EXPENSES (ID, E_DATE, AMMOUNT, PERSON, COMMENTS, T_ID) VALUES (@id, @date,@ammount, @person, @comments, @tid)",myMySqlConnection);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@date", date);
cmd.Parameters.AddWithValue("@ammount", ammount);
cmd.Parameters.AddWithValue("@person", person);
cmd.Parameters.AddWithValue("@comments", comments);
cmd.Parameters.AddWithValue("@tid", tid);
cmd.ExecuteNonQuery();
当我在datagridview中查看它们时:
string test = startdate1.ToString("yyyy:MM:dd");
string test2 = enddate1.ToString("yyyy:MM:dd");
DataTable table1 = new DataTable();
cmd1 = new MySqlCommand("SELECT E_DATE, AMMOUNT, PERSON, COMMENTS, T_ID FROM EXPENSES WHERE DATE (E_DATE) BETWEEN '" + test + "' AND '" + test2 + "' AND ID =@userid ORDER BY E_DATE ASC", myMySqlConnection);
cmd1.Parameters.AddWithValue("@userid", id);
da1.SelectCommand = cmd1;
da1.Fill(table1);
BindingSource bSource1 = new BindingSource();
bSource1.DataSource = table1;
dataGridView1.DataSource = bSource1;
现在我想在excel中导出它们:
Excel_12.Application oExcel_12 = null;
Excel_12.Workbook oBook = null;
Excel_12.Sheets oSheetsColl = null;collection
Excel_12.Worksheet oSheet = null;
Excel_12.Range oRange = null;
Object oMissing = System.Reflection.Missing.Value;
oExcel_12 = new Excel_12.Application();
oExcel_12.Visible = true;
oExcel_12.UserControl = true;
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
oBook = oExcel_12.Workbooks.Add(oMissing);
oSheetsColl = oExcel_12.Worksheets;
oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");
// Export titles
for (int j = 0; j < myDataGridView.Columns.Count; j++)
{
oRange = (Excel_12.Range)oSheet.Cells[4, j + 1];
oRange.Value2 = myDataGridView.Columns[j].HeaderText;
oRange.Cells[1].ColumnWidth = 13;
}
// Export data
for (int i = 0; i < myDataGridView.Rows.Count; i++)
{
for (int j = 1; j < myDataGridView.Columns.Count; j++)
{
oRange = (Excel_12.Range)oSheet.Cells[i + 5, j + 1];
oRange.Value2 = myDataGridView[j, i].Value;
}
}//This is for everything except the dates
for (int i = 0; i < myDataGridView.Rows.Count; i++)
{
for (int j = 0; j < 1; j++)
{
oRange = (Excel_12.Range)oSheet.Cells[i + 5, j + 1];
oRange.Cells[1].NumberFormat = "dd/MMM/yyyy;@";
DateTime test = Convert.ToDateTime( myDataGridView[j, i].ToString(),ci);
oRange.Value2 = test; //test.Remove(test.Length - 11);
// oRange.NumberFormat = "dd-MM-yyyy";
}
}//This is for the dates
//oBook.Close(false, oMissing, oMissing);
oBook = null;
//oExcel_12.Quit();
oExcel_12 = null;
// Collect garbage.
GC.Collect();
问题是,当区域设置设置为GREEK时,Excel无法识别日期的数字格式。如果区域设置设置为英语-US,则日期没有问题。如何让它识别日期而不必担心计算机的区域设置。任何帮助将真正受到赞赏,因为这些希腊语让我发疯。 最好的祝福 George Georgiou
答案 0 :(得分:1)
我认为你可以通过以下方式解决这个问题:
oRange = (Excel_12.Range)oSheet.Cells[i + 5, j + 1];
oRange.Cells[1].NumberFormat = "dd/MMM/yyyy;@";
DateTime test = Convert.ToDateTime( myDataGridView[j, i].ToString(),ci);
oRange.Value2 = test.ToOADate();
答案 1 :(得分:1)
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
这将返回当前文化是“el-GR”
然后:
string test = (Convert.ToDateTime(myDataGridView[j, i].Value).ToShortDateString());
DateTime asd = Convert.ToDateTime (test,ci);
oRange.NumberFormat = "dd-MMM-yyyy";
oRange.Value2 = asd;
我们获取值并将其作为字符串提供,然后根据文化信息(即el-GR)将其转换为datetime,并为excel提供日期值并按照您希望的方式对其进行格式化
答案 2 :(得分:0)
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);
// Change properties of the Workbook
ExcelApp.Columns.ColumnWidth = 20;
// Storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// Storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
ExcelApp.ActiveWorkbook.SaveCopyAs("F:\\Exportdata\\" + "DealEntry.xls");
ExcelApp.ActiveWorkbook.Saved = true;
MessageBox.Show("It's Saved on Path F:\\Exportdata\\DealEntry.Xls");
ExcelApp.Quit();
ExcelApp.Visible = true;
string workbookPath = "F:\\Exportdata\\" + "DealEntry.xls"; // Add your own path here
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = ExcelApp.Workbooks.Open(workbookPath, 0,
false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
}
catch { }