forked from hechenqingyuan/gitwms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcelHelper.cs
139 lines (133 loc) · 5.42 KB
/
ExcelHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
//using Microsoft.Office.Interop.Excel;
namespace Storage.Common
{
/// <summary>
/// EXcel帮助类
/// </summary>
public class ExcelHelper
{
private static string GetConnString(string path, string excelVersion)
{
string strConn = "";
if (excelVersion == "2003")
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
else //if (excelVersion == "2007")
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;";
strConn += @"Data Source=" + path + ";";
strConn += "Extended Properties=\"Excel 12.0 Xml;HDR=NO\";";
}
return strConn;
}
//加载Excel
public static DataSet LoadDataFromExcel(string filePath, string SheetName, string top = "", string excelVersion = "2003")
{
try
{
string strConn = GetConnString(filePath, excelVersion);
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
System.Data.DataTable dt = GetTableName(OleConn);
if (dt.Rows.Count <= 0)
throw new Exception("Excel中没有工作表!");
if (string.IsNullOrEmpty(SheetName))
throw new Exception("工作表不能为空");
bool isexistTable = false;
for (var rowIndex = 0; rowIndex < dt.Rows.Count;rowIndex++ )
{
if (SheetName == Convert.ToString(dt.Rows[rowIndex][2]))
{
isexistTable = true;
break;
}
}
if (!isexistTable) {
throw new Exception("工作表"+SheetName+"不存在");
}
String sql = "SELECT "+top+" * FROM [" + SheetName + "]";//可是更改Sheet名称,比如sheet2,等等
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet OleDsExcle = new DataSet();
OleDaExcel.Fill(OleDsExcle, SheetName);
OleConn.Close();
return OleDsExcle;
}
catch (Exception err)
{
throw err;
}
}
public static System.Data.DataTable GetTableName(OleDbConnection oledbconn1)
{
System.Data.DataTable dt = oledbconn1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
return dt;
}
/// <summary>
/// 获得所有的Excel工作表名称
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static System.Data.DataTable GetTableName(string path,string excelVersion="2003")
{
string strConn = GetConnString(path, excelVersion);
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
System.Data.DataTable dt = OleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
OleConn.Close();
return dt;
}
public static bool SaveDataTableToExcel(System.Data.DataTable excelTable, string filePath)
{
// Microsoft.Office.Interop.Excel.Application app =
// new Microsoft.Office.Interop.Excel.ApplicationClass();
// try
// {
// app.Visible = false;
// Workbook wBook = app.Workbooks.Add(true);
// Worksheet wSheet = wBook.Worksheets[1] as Worksheet;
// if (excelTable.Rows.Count > 0)
// {
// int row = 0;
// row = excelTable.Rows.Count;
// int col = excelTable.Columns.Count;
// for (int i = 0; i < row; i++)
// {
// for (int j = 0; j < col; j++)
// {
// string str = excelTable.Rows[i][j].ToString();
// wSheet.Cells[i + 2, j + 1] = str;
// }
// }
// }
// int size = excelTable.Columns.Count;
// for (int i = 0; i < size; i++)
// {
// wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName;
// }
// //设置禁止弹出保存和覆盖的询问提示框
// app.DisplayAlerts = false;
// app.AlertBeforeOverwriting = false;
// //保存工作簿
// wBook.Save();
// //保存excel文件
// app.Save(filePath);
// app.SaveWorkspace(filePath);
// app.Quit();
// app = null;
return true;
// }
// catch (Exception err)
// {
// throw err;
// }
// finally
// {
// }
}
}
}