1

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

4

3 回答 3

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)将其转换为日期时间,并为 excel 提供日期值并按照您想要的方式对其进行格式化

于 2012-06-11T08:21:30.973 回答
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();
于 2012-06-11T07:05:10.690 回答
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 { }
于 2013-03-15T07:35:16.850 回答