C#操作Excel文件(读取Excel,写入Excel)

来源:互联网 发布:koka方便面 知乎 编辑:程序博客网 时间:2024/04/19 19:59
2009-01-10 09:40

看到论坛里面不断有人提问关于读取excel和导入excel的相关问题。闲暇时间将我所知道的对excel的操作加以总结,现在共享大家,希望给大家能够给大家带了一定的帮助。
另外我们还要注意一些简单的问题1.excel文件只能存储65535行数据,如果你的数据大于65535行,那么就需要将excel分割存放了。2.关于乱码,这主要是字符设置问题。

1.加载Excel(读取excel内容)返回值是一个DataSet

  1.         //加载Excel
  2.         public static DataSet LoadDataFromExcel(string filePath)
  3.          {
  4.             try
  5.              {
  6.                 string strConn;
  7.                  strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
  8.                  OleDbConnection OleConn = new OleDbConnection(strConn);
  9.                  OleConn.Open();
  10.                  String sql = "SELECT * FROM   [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等
  11.                  OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
  12.                  DataSet OleDsExcle = new DataSet();
  13.                  OleDaExcel.Fill(OleDsExcle, "Sheet1");
  14.                  OleConn.Close();
  15.                 return OleDsExcle;
  16.              }
  17.             catch (Exception err)
  18.              {
  19.                  MessageBox.Show("数据绑定Excel失败!失败原因:" + err.Message, "提示信息",
  20.                      MessageBoxButtons.OK, MessageBoxIcon.Information);
  21.                 return null;
  22.              }
  23.          }

2.写入Excel内容,参数:excelTable是要导入excel的一个table表

  1.         public static bool SaveDataTableToExcel(System.Data.DataTable excelTable, string filePath)
  2.          {
  3.              Microsoft.Office.Interop.Excel.Application app =
  4.                 new Microsoft.Office.Interop.Excel.ApplicationClass();
  5.             try
  6.              {
  7.                  app.Visible = false;
  8.                  Workbook wBook = app.Workbooks.Add(true);
  9.                  Worksheet wSheet = wBook.Worksheets[1] as Worksheet;
  10.                 if (excelTable.Rows.Count > 0)
  11.                  {
  12.                     int row = 0;
  13.                      row = excelTable.Rows.Count;
  14.                     int col = excelTable.Columns.Count;
  15.                     for (int i = 0; i < row; i++)
  16.                      {
  17.                         for (int j = 0; j < col; j++)
  18.                          {
  19.                             string str = excelTable.Rows[i][j].ToString();
  20.                              wSheet.Cells[i + 2, j + 1] = str;
  21.                          }
  22.                      }
  23.                  }
  24.                 int size = excelTable.Columns.Count;
  25.                 for (int i = 0; i < size; i++)
  26.                  {
  27.                      wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName;
  28.                  }
  29.                 //设置禁止弹出保存和覆盖的询问提示框
  30.                  app.DisplayAlerts = false;
  31.                  app.AlertBeforeOverwriting = false;
  32.                 //保存工作簿
  33.                  wBook.Save();
  34.                 //保存excel文件
  35.                  app.Save(filePath);
  36.                  app.SaveWorkspace(filePath);
  37.                  app.Quit();
  38.                  app = null;
  39.                 return true;
  40.              }
  41.             catch (Exception err)
  42.              {
  43.                  MessageBox.Show("导出Excel出错!错误原因:" + err.Message, "提示信息",
  44.                      MessageBoxButtons.OK, MessageBoxIcon.Information);
  45.                 return false;
  46.              }
  47.             finally
  48.              {
  49.              }
  50.          }