//选择文件 OpenFileDialog dlg = new OpenFileDialog(); dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); dlg.Filter = "Office Execl2003(*.xls)|*.xls|Office Execl2007(*.xlsx)|*.xlsx"; dlg.FilterIndex = 1; dlg.RestoreDirectory = true; dlg.Multiselect = false; dlg.Title = CliUtils.GetMessage("FilerChooseImport"); if (dlg.ShowDialog() == DialogResult.OK) { Import(dlg.FileName); } //导入 private void Import(string fileName) { if (string.Compare(System.IO.Path.GetExtension(fileName).Replace(".", ""), "xls", true) == 0 || string.Compare(System.IO.Path.GetExtension(fileName).Replace(".", ""), "xlsx", true) == 0) { try { Workbook wb = new Workbook(); wb.Open(fileName); DataTable excelDt1 = wb.Worksheets[0].Cells.ExportDataTable(0, 0, wb.Worksheets[0].Cells.MaxRow + 1, wb.Worksheets[0].Cells.MaxColumn + 1); //excelDt1 此时保存的就是excel中的数据。 } catch { MessageBox.Show(CliUtils.GetMessage("fileformaterror")); return; } } else { MessageBox.Show("文件格式错误"); return; } }
C#winform导入数据
最新推荐文章于 2024-06-26 12:47:17 发布