文件流——Excel文件数据读写
2013-01-23 15:25
295 查看
(1)连接字符串
Excel97-2003:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'
Excel2007:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=False;IMEX=1'
(2)读取
返回值是一个数据集DataSet
1. public static DataSet LoadDataFromExcel(string filePath)
2. {
3. try
4. {
5. string strConn;
6. strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
7. OleDbConnection OleConn = new OleDbConnection(strConn);
8. OleConn.Open();
9. String sql = "SELECT * FROM [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等
10.
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.}
(3)写入
参数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.
25. int size = excelTable.Columns.Count;
26. for (int i = 0; i < size; i++)
27. {
28. wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName;
29. }
30. //设置禁止弹出保存和覆盖的询问提示框
31. app.DisplayAlerts = false;
32. app.AlertBeforeOverwriting = false;
33. //保存工作簿
34. wBook.Save();
35. //保存excel文件
36. app.Save(filePath);
37. app.SaveWorkspace(filePath);
38. app.Quit();
39. app = null;
40. return true;
41. }
42. catch (Exception err)
43. {
44. MessageBox.Show("导出Excel出错!错误原因:" + err.Message, "提示信息",
45. MessageBoxButtons.OK, MessageBoxIcon.Information);
46. return false;
47. }
48. finally
49. {
50. }
}
Excel97-2003:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'
Excel2007:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=False;IMEX=1'
(2)读取
返回值是一个数据集DataSet
1. public static DataSet LoadDataFromExcel(string filePath)
2. {
3. try
4. {
5. string strConn;
6. strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
7. OleDbConnection OleConn = new OleDbConnection(strConn);
8. OleConn.Open();
9. String sql = "SELECT * FROM [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等
10.
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.}
(3)写入
参数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.
25. int size = excelTable.Columns.Count;
26. for (int i = 0; i < size; i++)
27. {
28. wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName;
29. }
30. //设置禁止弹出保存和覆盖的询问提示框
31. app.DisplayAlerts = false;
32. app.AlertBeforeOverwriting = false;
33. //保存工作簿
34. wBook.Save();
35. //保存excel文件
36. app.Save(filePath);
37. app.SaveWorkspace(filePath);
38. app.Quit();
39. app = null;
40. return true;
41. }
42. catch (Exception err)
43. {
44. MessageBox.Show("导出Excel出错!错误原因:" + err.Message, "提示信息",
45. MessageBoxButtons.OK, MessageBoxIcon.Information);
46. return false;
47. }
48. finally
49. {
50. }
}
相关文章推荐
- matlab 读写其他格式数据文件(excel)
- Java读写Excel文件中数据的简便方法
- MATLAB读写Excel文件中的数据
- python数据分析之(4)读写数据文件CSV,EXCEL等
- R读写Excel文件中数据的方法
- R读写Excel文件中数据的方法
- MATLAB如何读写Excel数据文件
- asp.net 生成导出word表单 ,导出excel; dataTable生成xls文件,返回前台下载;asp.net启动excel错误 80070005;excel-xls columnName 不能改变; 读写excel的开源利器NPOI; 设置excel Cell的数据类型;
- C语言通过ODBC读写Excel表格文件(ODBC)
- Java基本功练习十四(字符串和文本IO【文件读写数据、字符串生成器、String类方法、相关技巧】)
- Linux内核中读写文件数据的方法
- 数据Cocos2d-x常用功能-Cocos2d-x常用工具:计时器、数据读写、文件读写(共6部分)
- MATLAB怎样有效读取excel文件中的数据?
- Java POI 读写Excel 文件简单实现
- 关于Excel操作编写的一个软件设计构思案例[连载] --如何把处理好后的数据导出Excel文件中(含背景\字体颜色设置)
- load data infile将excel文件中的数百万条数据在1分钟内导入数据库
- 将excel表中的数据写到txt文本文件的Python脚本
- 将Excel文件数据导入到SqlServer数据库的三种方案
- datagrid数据导出到excel文件给客户端下载的几种方法
- 【转】VS2008环境使用MFC导入Excel文件中的数据