C#winform导入数据
来源:互联网 发布:手机网络赚钱资源 编辑:程序博客网 时间:2024/05/01 16:23
//选择文件 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; } }