我的问题是,我有 mysql 数据库,当我插入数据时:
字符串日期 = 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 无法识别日期的数字格式。如果区域设置设置为英语 - 美国,则日期没有问题。我怎样才能让它识别日期而不必担心计算机的区域设置。任何帮助将不胜感激,因为这些希腊语让我发疯。最好的问候乔治乔治