0

我想知道是否可以从 excel 中的查询行中设置 DateTimePicker 的值。我尝试过不同的方法,但从未接近过,有人帮忙吗?

此外,这一切都显示在 Windows 窗体上

4

1 回答 1

0
    public void ReadAndWriteToExcel()
    {
        string myPath = @"C:\Excel.xls";
        FileInfo fi = new FileInfo(myPath);
        if (!fi.Exists)
        {
            Console.Out.WriteLine("file doesn't exists!");
        }
        else
        {
            var excelApp = new Microsoft.Office.Interop.Excel.Application();
            var workbook = excelApp.Workbooks.Open(myPath);
            Worksheet worksheet = workbook.ActiveSheet as Worksheet;

            // To write to excel
            Microsoft.Office.Interop.Excel.Range range = worksheet.Cells[1, 1] as Range;
            DateTime dt = dateTimePicker1.Value;
            range.NumberFormat = "dd/MMM/yyyy";
            range.Value2 = dt;

            // To read,
            var date = worksheet.Cells[1, 1].Value;
            Console.Out.WriteLine(date.ToString());

            workbook.Save();
            workbook.Close();
        }
    }

您必须将“Microsoft.Office.Core”和“Microsoft.Office.Interop.Excel”引用添加到您的项目并使用它们。

于 2013-04-25T09:27:19.420 回答