《GemBox.ExcelLite "完美"的导入Excel的控件》中说到该控件不支持数据集的导入。

这几天,看到网上有很多Helper的代码来辅助实现包括这个的一系列功能。

代码如下:

 

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Text;
4  using System.Data;
5  using System.Data.OleDb;
6  using System.Web.UI;
7
8  using GemBox.ExcelLite;
9
10  namespace UserExcelLite.WebUI
11 {
12 /// <summary>
13 /// Excel 操作类
14 /// 采用GemBox.ExcelLite第三方控件
15 /// </summary>
16   public class GemBoxExcelLiteHelper
17 {
18 public GemBoxExcelLiteHelper() { }
19
20 #region 在线生成Excel
21 /// <summary>
22 /// 在线生成Excel
23 /// </summary>
24 /// <param name="fileName">文件名称 注:必须加这个方法
25 /// public override void VerifyRenderingInServerForm(Control control)
26 /// {
27 /// Confirms that an HtmlForm control is rendered for
28 /// }
29 /// </param>
30 /// <param name="page"></param>
31 /// <param name="gridView"></param>
32   public static void OnlineSaveExcel(string fileName, System.Web.UI.Page page, System.Web.UI.WebControls.GridView gridView)
33 {
34 page.Response.Clear();
35 page.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
36 page.Response.Charset = "gb2312";
37 page.Response.ContentType = "application/vnd.xls";
38 System.IO.StringWriter stringWrite = new System.IO.StringWriter();
39 System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
40 //先设置分页为false
41 gridView.AllowPaging = false;
42 gridView.RenderControl(htmlWrite);
43 page.Response.Write(stringWrite.ToString());
44 page.Response.End();
45 }
46 #endregion
47
48 #region 生成Excel
49 /// <summary>
50 /// 生成Excel
51 /// </summary>
52 /// <param name="path">绝对路径</param>
53 /// <param name="page"></param>
54 /// <param name="isDownload">是否提供下载,true是,false否</param>
55 /// <param name="isDelete">是否删除本地生成的Excel,true是,false否</param>
56 /// <param name="titles">Excel标题</param>
57 /// <param name="gridView">数据</param>
58 public static void SaveExcel(string path, System.Web.UI.Page page, bool isDownload, bool isDelete, System.Web.UI.WebControls.GridView gridView)
59 {
60 try
61 {
62 //保存在本地
63 SaveToXls(path, gridView);
64 if (isDownload)
65 {
66 //提供下载
67 UploadExcel(path, page, isDelete);
68 }
69 }
70 catch (Exception ex)
71 {
72 throw ex;
73 }
74 }
75 #endregion
76
77 #region 生成Excel
78
79 /// <summary>
80 /// 生成Excel
81 /// </summary>
82 /// <param name="path">绝对路径</param>
83 /// <param name="page"></param>
84 /// <param name="isDownload">是否提供下载,true是,false否</param>
85 /// <param name="isDelete">是否删除本地生成的Excel,true是,false否</param>
86 /// <param name="titles">Excel标题</param>
87 /// <param name="ds">数据</param>
88 public static void SaveExcel(string path, System.Web.UI.Page page, bool isDownload, bool isDelete, IList<String> titles, DataSet ds)
89 {
90 try
91 {
92 //保存在本地
93 SaveToXls(path, titles, ds);
94 if (isDownload)
95 {
96 //提供下载
97 UploadExcel(path, page, isDelete);
98 }
99 }
100 catch (Exception ex)
101 {
102 throw ex;
103 }
104 }
105 #endregion
106
107 #region 提供下载
108 /// <summary>
109 /// 提供下载
110 /// </summary>
111 /// <param name="path"></param>
112 /// <param name="page"></param>
113 /// <param name="isDelete"></param>
114 private static void UploadExcel(string path, System.Web.UI.Page page, bool isDelete)
115 {
116 System.IO.FileInfo file = new System.IO.FileInfo(path);
117 page.Response.Clear();
118 page.Response.Charset = "GB2312";
119 page.Response.ContentEncoding = System.Text.Encoding.UTF8;
120 // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
121 page.Response.AddHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(file.Name));
122 // 添加头信息,指定文件大小,让浏览器能够显示下载进度
123 page.Response.AddHeader("Content-Length", file.Length.ToString());
124
125 // 指定返回的是一个不能被客户端读取的流,必须被下载
126 page.Response.ContentType = "application/ms-excel";
127
128 // 把文件流发送到客户端
129 page.Response.WriteFile(file.FullName);
130
131 page.Response.Flush();
132 if (isDelete)
133 {
134 System.IO.File.Delete(path);
135 }
136
137 // 停止页面的执行
138 page.Response.End();
139 }
140 #endregion
141
142 #region 保存在本地
143 /// <summary>
144 /// 保存在本地
145 /// </summary>
146 /// <param name="path">绝对路径</param>
147 /// <param name="gridView">数据</param>
148 private static void SaveToXls(string path, System.Web.UI.WebControls.GridView gridView)
149 {
150
151 ExcelFile excelFile = new ExcelFile();
152 ExcelWorksheet sheet = excelFile.Worksheets.Add("Sheet1");
153
154 int columns = gridView.HeaderRow.Cells.Count;
155 int rows = gridView.Rows.Count;
156 for (int j = 0; j < columns; j++)
157 {
158 sheet.Cells[0, j].Value = gridView.HeaderRow.Cells[j].Text;
159 //修改Excel表头字体,现在为黑体12号字体
160 sheet.Cells[0, j].Style.Font.Size = 240;
161 sheet.Cells[0, j].Style.Font.Name = "黑体";
162 }
163
164 for (int i = 0; i < rows; i++)
165 {
166 for (int j = 0; j < columns; j++)
167 {
168 sheet.Cells[i + 1, j].Value = gridView.Rows[i].Cells[j].Text;
169 }
170 }
171 excelFile.SaveXls(path);
172 }
173
174 //
175 //网上源码
176 //
177 //private static void SaveToXls(string path, System.Web.UI.WebControls.GridView gridView)
178 //{
179
180 // ExcelFile excelFile = new ExcelFile();
181 // ExcelWorksheet sheet = excelFile.Worksheets.Add("Sheet1");
182
183 // int columns = gridView.Columns.Count;
184 // int rows = gridView.Rows.Count;
185 // for (int j = 0; j < columns; j++)
186 // {
187 // sheet.Cells[0, j].Value = gridView.Columns[j].HeaderText;
188 // }
189
190 // for (int i = 0; i < rows; i++)
191 // {
192 // for (int j = 0; j < columns; j++)
193 // {
194 // sheet.Cells[i + 1, j].Value = gridView.Rows[i].Cells[j].Text;
195 // }
196 // }
197 // excelFile.SaveXls(path);
198 //}
199 #endregion
200
201 #region 保存在本地
202 /// <summary>
203 /// 保存在本地
204 /// </summary>
205 /// <param name="path">绝对路径</param>
206 /// <param name="titles">Excel标题</param>
207 /// <param name="ds">数据</param>
208 private static void SaveToXls(string path, IList<String> titles, DataSet ds)
209 {
210 System.Data.DataTable dt = ds.Tables[0];
211 ExcelFile excelFile = new ExcelFile();
212 ExcelWorksheet sheet = excelFile.Worksheets.Add("Sheet1");
213
214
215 int columns = dt.Columns.Count;
216 int rows = dt.Rows.Count;
217 int index = 0;
218 if (titles != null && titles.Count > 0)
219 {
220 for (int i = 0; i < titles.Count; i++)
221 {
222
223 sheet.Cells[0, i].Value = titles[i];
224 }
225 index++;
226 }
227
228 for (int i = 0; i < rows; i++)
229 {
230 for (int j = 0; j < columns; j++)
231 {
232 sheet.Cells[i + index, j].Value = dt.Rows[i][j].ToString();
233 }
234 }
235 excelFile.SaveXls(path);
236
237 }
238 #endregion
239
240 #region 从Excel导入DataSet
241 /// <summary>
242 /// 从Excel导入DataSet
243 /// </summary>
244 /// <param name="path">路径</param>
245 /// <param name="sheetName">sheetName</param>
246 /// <param name="hDRString">第一列是否为列名</param>
247 /// <param name="tableName">表名</param>
248 /// <returns></returns>
249 public static DataSet LoadExcel(string path, string sheetName, HDRString hDRString, string tableName)
250 {
251
252 //HDR和IMEX也一定要配合使用,IMEX=1应该是将所有的列全部视为文本,HDR:YES第一行作为列名,NO表示第一行不为列名
253 string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + path + ";Extended Properties='Excel 8.0; HDR=" + (hDRString == HDRString.YES ? "YES" : "NO") + "; IMEX=1'";
254 OleDbConnection conn = new OleDbConnection(connectionString);
255
256 OleDbDataAdapter odda = new OleDbDataAdapter("select * from [" + sheetName + "$]", conn);
257 DataSet ds = new DataSet();
258 odda.Fill(ds, tableName);
259 odda.Dispose();
260 conn.Close();
261 return ds;
262 }
263 #endregion
264 }
265 public enum HDRString
266 {
267 YES, NO
268 }
269 }

 

 

请注意“保存在本”地这个函数,也就是SaveToXls(string path, System.Web.UI.WebControls.GridView gridView)函数,这个函数下面有一段被我注释掉的,这些是网上的源码,不过我在测试的时候用网上的一直有错误,所以改成上面的。

 

有了这个类我们可以很方便的使用GemBox.ExcelLite这个控件,不过也有需要注意的地方。

首先,使用这个类的话就不能在GV中使用模板,确切的说是在GV中的数据要导入Excel的数据不能用模板,否则需要对类进行修改。

其次,不能很方便的修改到处Excel表格的格式文字大小等等一系列信息。

 

如果只是简单的使用,这个类还是很有帮助的,不过想复杂点的话还是直接使用控件自带的类进行相应的操作吧,不过看看这个类能更好的理解GemBox.ExcelLite的使用。

 

我使用上面辅助类的代码:

 

代码
1 //使用结果集导出Excel
2 IList<string> titles = new List<string>();
3 titles.Add("编号");
4 titles.Add("姓名");
5 titles.Add("性别");
6 GemBoxExcelLiteHelper.SaveExcel("C:\\ddd.xls", this, true, true, titles, getStudent());
7
8 //使用GV导出Excel
9 GemBoxExcelLiteHelper.SaveExcel("C:\\ddd.xls", this, true, true, gv_student);

 

GemBox.ExcelLite源码下载

posted on 2010-07-16 16:32  Rlecir  阅读(3151)  评论(5编辑  收藏  举报