![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary> 2 /// 利用com组件读取excel文件 3 /// </summary> 4 /// <param name="fileName">文件路径</param> 5 /// <returns></returns> 6 private System.Data.DataTable ExcelToDataSet(string fileName) 7 { 8 //创建Excel对象 9 Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); 10 //获取工作表集合 11 Microsoft.Office.Interop.Excel.Workbooks myBooks = myExcel.Application.Workbooks; 12 object oMissing = System.Reflection.Missing.Value; 13 14 //打开指定路径的excel文件 15 Microsoft.Office.Interop.Excel.Workbook myBook = myBooks.Open(fileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); 16 17 //得到工作表的数量 18 int sheetint = myBook.Worksheets.Count; 19 20 Microsoft.Office.Interop.Excel.Worksheet mySheet = (Microsoft.Office.Interop.Excel.Worksheet)myBook.Worksheets[1]; 21 //得到行数 22 int rowsint = mySheet.UsedRange.Cells.Rows.Count; 23 //得到列数 24 int columnsint = mySheet.UsedRange.Cells.Columns.Count; 25 System.Data.DataTable dt = new System.Data.DataTable("mytable"); 26 for (int i = 1; i <=columnsint; i++) 27 { 28 Microsoft.Office.Interop.Excel.Range r = (Microsoft.Office.Interop.Excel.Range)mySheet.Cells[1, i]; 29 dt.Columns.Add(r.Text.ToString(), System.Type.GetType("System.String")); 30 } 31 DataSet myDs = new DataSet(); 32 myDs.Tables.Add(dt); 33 DataRow myRow; 34 myDs.Clear(); 35 36 //第一行以作为列标题,从第二行开始读取 37 for (int i = 2; i < rowsint; i++) 38 { 39 myRow = myDs.Tables["mytable"].NewRow(); 40 for (int j = 1; j <= columnsint; j++) 41 { 42 Microsoft.Office.Interop.Excel.Range r = (Microsoft.Office.Interop.Excel.Range)mySheet.Cells[i, j]; 43 string strValue = r.Text.ToString(); 44 string aa = strValue; 45 string columnname = "F" + j.ToString(); 46 myRow[j-1] = strValue; 47 48 } 49 myDs.Tables["mytable"].Rows.Add(myRow); 50 } 51 52 return myDs.Tables["mytable"]; 53 54 55 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary> 2 /// 获取datagridviews的数据 3 /// </summary> 4 /// <param name="dataGridView"></param> 5 /// <returns></returns> 6 private System.Data.DataTable GetExcelTable(DataGridView dgv) 7 { 8 System.Data.DataTable dt = new System.Data.DataTable(); 9 //添加列 10 for (int count = 0; count < dgv.Columns.Count; count++) 11 { 12 DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString()); 13 dt.Columns.Add(dc); 14 } 15 //添加行 16 for (int count = 0; count < dgv.Rows.Count; count++) 17 { 18 DataRow dr = dt.NewRow(); 19 for (int countsub = 0; countsub < dgv.Columns.Count; countsub++) 20 { 21 dr[countsub] = dgv.Rows[count].Cells[countsub].Value.ToString(); 22 } 23 dt.Rows.Add(dr); 24 } 25 return dt; 26 } 27 28 /// <summary> 29 /// 写入excel内容 30 /// </summary> 31 /// <param name="excelTable">导入excel的table表</param> 32 /// <param name="filePath">保存路径</param> 33 /// <returns></returns> 34 public static bool SaveDataTableToExcel(System.Data.DataTable excelTable, string filePath) 35 { 36 //判读网格中是否有数据 37 if (excelTable.Rows.Count == 0 || filePath == string.Empty) 38 { 39 return false; 40 } 41 42 //创建Excel操作对象 43 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); 44 //行号 45 int rowindex = 1; 46 //列号 47 int colindex = 0; 48 Workbook work = excel.Workbooks.Add(true); 49 //遍历列,设置excel的列标题 50 foreach (DataColumn col in excelTable.Columns) 51 { 52 colindex++; 53 excel.Cells[1, colindex] = col.ColumnName; 54 } 55 foreach (DataRow row in excelTable.Rows) 56 { 57 rowindex++; 58 colindex = 0; 59 foreach (DataColumn col in excelTable.Columns) 60 { 61 colindex++; 62 excel.Cells[rowindex, colindex] = row[col.ColumnName].ToString(); 63 } 64 } 65 excel.Visible = false; 66 //保存Excel 67 excel.ActiveWorkbook.SaveAs(filePath, XlFileFormat.xlExcel9795, null, null, false, false,XlSaveAsAccessMode.xlNoChange, null, null, null, null, null); 68 excel.Quit(); 69 excel = null; 70 GC.Collect(); 71 MessageBox.Show("数据已成功导出Excel表中"); 72 return true; 73 74 }