《C# Excel表的导入导出》由会员分享,可在线阅读,更多相关《C# Excel表的导入导出(5页珍藏版)》请在金锄头文库上搜索。
1、首先,我们先说下Excel的导入:Excel导入分为3个步骤,1、获取.xls文件的路径(即选择目标文件)2、选择sheet,每个excel都可以有多个sheet,我们获取第一个sheet名称并读取其中的内容。3、读取Excel表中的数据,在以下例子中,我把Excel中的表存为DataSet对象。代码:前提条件:(引用Microsoft.Office.Interop.Excel)1、选择Excel目录public static string GetExcelPath() using (OpenFileDialog dlgFile = new OpenFileDialog() dlgFile.C
2、heckFileExists = false; dlgFile.Filter = Excel files|*.xls|All files|*.*; if (dlgFile.ShowDialog() = DialogResult.OK) return dlgFile.FileName; return string.Empty; 2、获取第一个Sheet名称public string GetExcelFirstTableName(string excelFileName) string tableName = null; if (File.Exists(excelFileName) using (
3、OleDbConnection conn = new OleDbConnection(Provider=Microsoft.Jet. + OLEDB.4.0;Extended Properties=Excel 8.0;Data Source= + excelFileName) try conn.Open(); DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); tableName = dt.Rows02.ToString().Trim(); catch MessageBox.Show(请先关闭Excel!
4、, 提示, MessageBoxButtons.OK, MessageBoxIcon.Warning); return tableName; 3、读取.xls表的内容并存为DataSetpublic static DataSet ReadExcel(string Path, string Sheet) string connStr = Provider=Microsoft.Jet.OLEDB.4.0; + Extended Properties=Excel 8.0; + data source= + Path; DataSet dt = new DataSet(); try OleDbData
5、Adapter dr = new OleDbDataAdapter(select * from + Sheet + , connStr); dr.Fill(dt); catch (Exception e) e.ToString(); return dt; 以上为导入,接着是导出 导出分为2步,1、新建Excel工作表2、导出Excel表(分2步是为了方便选择导出内容,在第二步可选择所有导出或选择导出) 代码:前提条件:(引用Microsoft.Office.Interop.Excel)1、新建Excel工作表public static Microsoft.Office.Interop.Exce
6、l.Application NewExcel(string head) Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();/新的excel文档 excel.Application.Workbooks.Add(true); /新的工作表 excel.Visible = true; excel.Cells1, 1 = head; return excel; 2、导出Excel表完全导出 public static void ExportExcel(M
7、icrosoft.Office.Interop.Excel._Application excel,DataGridView dgv) try for (int i = 0; i dgv.ColumnCount; i+) /写入DataGridView列标题到Excel表 excel.Cells2, i + 1 = dgv.Columnsi.HeaderText.ToString().Trim(); for (int row = 0; row dgv.RowCount; row+) /写入DataGridView各行内容到Excel表 for (int col = 0; col dgv.Colu
8、mnCount; col+) excel.Cellsrow + 3, col + 1 = dgv.Rowsrow.Cellscol.Value.ToString().Trim(); catch (Exception e) e.ToString(); 选择行和列导出 public static void ExportExcel(Microsoft.Office.Interop.Excel._Application excel, DataGridView dgv,int startcolnum,int endcolnum,int startrownum,int endrownum) if (end
9、colnum startcolnum) MessageBox.Show(结束列不能小于起始列, 错误, MessageBoxButtons.OK, MessageBoxIcon.Error); else for (int i = 0; i endcolnum - startcolnum + 1; i+) /写入DataGridView列标题到Excel表 excel.Cells2, i + 1 = dgv.Columnsi + startcolnum-1.HeaderText.ToString().Trim(); if (endrownum startrownum) MessageBox.Sh
10、ow(结束行不能小于起始行,错误,MessageBoxButtons.OK,MessageBoxIcon.Error); else for (int row = 0; row endrownum - startrownum+1; row+) /写入DataGridView各行内容到Excel表 for (int col = 0; col endcolnum - startcolnum + 1; col+) excel.Cellsrow + 3, col + 1 = dgv.Rowsrow + startrownum - 1.Cellscol + startcolnum - 1.Value.ToString().Trim();