1 /******************************************************************
2 *
3 *
4 * 描 述:
5 * 導入導出Excel通用類
6 * 版 本: V1.0
7 * 環 境: VS2005
8 ******************************************************************/
9 using System;
10 using System.Collections.Generic;
11 using System.Text;
12 using System.Windows.Forms;
13 using Excel = Microsoft.Office.Interop.Excel;
14 using System.Data;
15 using System.Drawing;
16 using System.Collections;
17 using System.Diagnostics;
18 using System.Data.OleDb;
19
20 namespace LingDang.CRM.UI.Client
21 {
22 public class ExcelIO:IDisposable
23 {
24 #region Constructors
25 private ExcelIO()
26 {
27 status = IsExistExecl() ? 0 : -1;
28 }
29
30 public static ExcelIO GetInstance()
31 {
32 //if(instance == null)
33 //{
34 // lock (syncRoot)
35 // {
36 // if(instance == null)
37 // {
38 // instance = new ExcelIO();
39 // }
40 // }
41 //}
42 //return instance;
43 return new ExcelIO();
44 }
45 #endregion
46
47 #region Fields
48 private static ExcelIO instance;
49 private static readonly object syncRoot = new object();
50 private string returnMessage;
51 private Excel.Application xlApp;
52 private Excel.Workbooks workbooks = null;
53 private Excel.Workbook workbook = null;
54 private Excel.Worksheet worksheet = null;
55 private Excel.Range range = null;
56 private int status = -1;
57 private bool disposed = false;//是否已經釋放資源的標記
58 #endregion
59
60 #region Properties
61 /// <summary>
62 /// 返回信息
63 /// </summary>
64 public string ReturnMessage
65 {
66 get { return returnMessage; }
67 }
68
69 /// <summary>
70 /// 狀態:0-正常,-1-失敗 1-成功
71 /// </summary>
72 public int Status
73 {
74 get { return status;}
75 }
76 #endregion
77
78 #region Methods
79 /// <summary>
80 /// 判斷是否安裝Excel
81 /// </summary>
82 /// <returns></returns>
83 protected bool IsExistExecl()
84 {
85 try
86 {
87 xlApp = new Excel.Application();
88 if (xlApp == null)
89 {
90 returnMessage = "無法創建Excel對象,可能您的計算機未安裝Excel!";
91 return false;
92 }
93 }
94 catch (Exception ex)
95 {
96 returnMessage = "請正確安裝Excel!";
97 //throw ex;
98 return false;
99 }
100
101 return true;
102 }
103
104 /// <summary>
105 /// 獲得保存路徑
106 /// </summary>
107 /// <returns></returns>
108 public static string SaveFileDialog()
109 {
110 SaveFileDialog sfd = new SaveFileDialog();
111 sfd.DefaultExt = "xls";
112 sfd.Filter = "Excel文件(*.xls)|*.xls";
113 if (sfd.ShowDialog() == DialogResult.OK)
114 {
115 return sfd.FileName;
116 }
117 return string.Empty;
118 }
119
120 /// <summary>
121 /// 獲得打開文件的路徑
122 /// </summary>
123 /// <returns></returns>
124 public static string OpenFileDialog()
125 {
126 OpenFileDialog ofd = new OpenFileDialog();
127 ofd.DefaultExt = "xls";
128 ofd.Filter = "Excel文件(*.xls)|*.xls";
129 if (ofd.ShowDialog() == DialogResult.OK)
130 {
131 return ofd.FileName;
132 }
133 return string.Empty;
134 }
135
136 /// <summary>
137 /// 設置單元格邊框
138 /// </summary>
139 protected void SetCellsBorderAround()
140 {
141 range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null);
142 //if (dt.Rows.Count > 0)
143 //{
144 // range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
145 // range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
146 // range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
147 //}
148 //if (dt.Columns.Count > 1)
149 {
150 range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
151 range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
152 range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
153 }
154 }
155
156 /// <summary>
157 /// 將DataTable導出Excel
158 /// </summary>
159 /// <param name="dt">數據集</param>
160 /// <param name="saveFilePath">保存路徑</param>
161 /// <param name="reportName">報表名稱</param>
162 /// <returns>是否成功</returns>
163 public bool DataTableToExecl(DataTable dt, string saveFileName, string reportName)
164 {
165 //判斷是否安裝Excel
166 bool fileSaved = false;
167 if(status == -1) return fileSaved;
168 //判斷數據集是否為null
169 if (dt == null)
170 {
171 returnMessage = "無引出數據!";
172 return false;
173 }
174 //判斷保存路徑是否有效
175 if (!saveFileName.Contains(":"))
176 {
177 returnMessage = "引出路徑有誤!請選擇正確路徑!";
178 return false;
179 }
180
181 //創建excel對象
182 workbooks = xlApp.Workbooks;
183 workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
184 worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
185 worksheet.Cells.Font.Size = 10;
186 worksheet.Cells.NumberFormat = "@";
187 long totalCount = dt.Rows.Count;
188 long rowRead = 0;
189 float percent = 0;
190 int rowIndex = 0;
191
192 //第一行為報表名稱,如果為null則不保存該行
193 ++rowIndex;
194 worksheet.Cells[rowIndex, 1] = reportName;
195 range = (Excel.Range)worksheet.Cells[rowIndex, 1];
196 range.Font.Bold = true;
197
198 //寫入字段(標題)
199 ++rowIndex;
200 for (int i = 0; i < dt.Columns.Count; i++)
201 {
202 worksheet.Cells[rowIndex,i+1] = dt.Columns[i].ColumnName;
203 range = (Excel.Range)worksheet.Cells[rowIndex, i + 1];
204
205 range.Font.Color = ColorTranslator.ToOle(Color.Blue);
206 range.Interior.Color = dt.Columns[i].Caption == "表體" ? ColorTranslator.ToOle(Color.SkyBlue) : ColorTranslator.ToOle(Color.Yellow);
207 }
208
209 //寫入數據
210 ++rowIndex;
211 for (int r = 0; r < dt.Rows.Count; r++)
212 {
213 for (int i = 0; i < dt.Columns.Count; i++)
214 {
215 worksheet.Cells[r + rowIndex, i + 1] = dt.Rows[r][i].ToString();
216 }
217 rowRead++;
218 percent = ((float)(100 * rowRead)) / totalCount;
219 }
220
221 //畫單元格邊框
222 range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dt.Rows.Count + 2, dt.Columns.Count]);
223 this.SetCellsBorderAround();
224
225 //列寬自適應
226 range.EntireColumn.AutoFit();
227
228 //保存文件
229 if (saveFileName != "")
230 {
231 try
232 {
233 workbook.Saved = true;
234 workbook.SaveCopyAs(saveFileName);
235 fileSaved = true;
236 }
237 catch (Exception ex)
238 {
239 fileSaved = false;
240 returnMessage = "導出文件時出錯,文件可能正被打開!\n" + ex.Message;
241 }
242 }
243 else
244 {
245 fileSaved = false;
246 }
247
248 //釋放Excel對應的對象(除xlApp,因為創建xlApp很花時間,所以等析構時才刪除)
249 //Dispose(false);
250 Dispose();
251 return fileSaved;
252 }
253
254 /// <summary>
255 /// 導入EXCEL到DataSet
256 /// </summary>
257 /// <param name="fileName">Excel全路徑文件名</param>
258 /// <returns>導入成功的DataSet</returns>
259 public DataSet ImportExcel(string fileName)
260 {
261 if (status == -1) return null;
262 //判斷文件是否被其他進程使用
263 try
264 {
265 workbook = xlApp.Workbooks.Open(fileName, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);
266 worksheet = (Excel.Worksheet)workbook.Worksheets[1];
267 }
268 catch
269 {
270 returnMessage = "Excel文件處於打開狀態,請保存關閉";
271 return null;
272 }
273
274 //獲得所有Sheet名稱 www.2cto.com
275 int n = workbook.Worksheets.Count;
276 string[] sheetSet = new string[n];
277 ArrayList al = new ArrayList();
278 for (int i = 0; i < n; i++)
279 {
280 sheetSet[i] = ((Excel.Worksheet)workbook.Worksheets[i+1]).Name;
281 }
282
283 //釋放Excel相關對象
284 Dispose();
285
286 //把EXCEL導入到DataSet
287 DataSet ds = null;
288 //string connStr = " Provider = Microsoft.ACE.OLEDB.12.0 ; Data Source = " + fileName + ";Extended Properties=\"Excel 12.0;HDR=No;IMEX=1;\"";
289 List<string> connStrs = new List<string>();
290 connStrs.Add("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + fileName + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1;\"");
291 connStrs.Add("Provider = Microsoft.ACE.OLEDB.12.0 ; Data Source = " + fileName + ";Extended Properties=\"Excel 12.0;HDR=No;IMEX=1;\"");
292 foreach (string connStr in connStrs)
293 {
294 ds = GetDataSet(connStr, sheetSet);
295 if (ds != null) break;
296 }
297 return ds;
298 }
299
300 /// <summary>
301 /// 通過olddb獲得dataset
302 /// </summary>
303 /// <param name="connectionstring"></param>
304 /// <returns></returns>
305 protected DataSet GetDataSet(string connStr, string[] sheetSet)
306 {
307 DataSet ds = null;
308 using (OleDbConnection conn = new OleDbConnection(connStr))
309 {
310 try
311 {
312 conn.Open();
313 OleDbDataAdapter da;
314 ds = new DataSet();
315 for (int i = 0; i < sheetSet.Length; i++)
316 {
317 string sql = "select * from [" + sheetSet[i] + "$] ";
318 da = new OleDbDataAdapter(sql, conn);
319 da.Fill(ds, sheetSet[i]);
320 da.Dispose();
321 }
322 conn.Close();
323 conn.Dispose();
324 }
325 catch (Exception ex)
326 {
327 return null;
328 }
329 }
330 return ds;
331 }
332
333 /// <summary>
334 /// 釋放Excel對應的對象資源
335 /// </summary>
336 /// <param name="isDisposeAll"></param>
337 protected virtual void Dispose(bool disposing)
338 {
339 try
340 {
341 if (!disposed)
342 {
343 if (disposing)
344 {
345 if (range != null)
346 {
347 System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
348 range = null;
349 }
350 if (worksheet != null)
351 {
352 System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
353 worksheet = null;
354 }
355 if (workbook != null)
356 {
357 System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
358 workbook = null;
359 }
360 if (workbooks != null)
361 {
362 xlApp.Application.Workbooks.Close();
363 System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
364 workbooks = null;
365 }
366 if (xlApp != null)
367 {
368 xlApp.Quit();
369 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
370 }
371 int generation = GC.GetGeneration(xlApp);
372 System.GC.Collect(generation);
373 }
374
375 //非托管資源的釋放
376 //KillExcel();
377 }
378 disposed = true;
379 }
380 catch (Exception e)
381 {
382 throw e;
383 }
384 }
385
386 /// <summary>
387 /// 會自動釋放非托管的該類實例的相關資源
388 /// </summary>
389 public void Dispose()
390 {
391 try
392 {
393 Dispose(true);
394 //告訴垃圾回收器,資源已經被回收
395 GC.SuppressFinalize(this);
396 }
397 catch (Exception e)
398 {
399 throw e;
400 }
401 }
402
403 /// <summary>
404 /// 關閉
405 /// </summary>
406 public void Close()
407 {
408 try
409 {
410 this.Dispose();
411 }
412 catch (Exception e)
413 {
414
415 throw e;
416 }
417 }
418
419 /// <summary>
420 /// 析構函數
421 /// </summary>
422 ~ExcelIO()
423 {
424 try
425 {
426 Dispose(false);
427 }
428 catch (Exception e)
429 {
430 throw e;
431 }
432 }
433
434 /// <summary>
435 /// 關閉Execl進程(非托管資源使用)
436 /// </summary>
437 private void KillExcel()
438 {
439 try
440 {
441 Process[] ps = Process.GetProcesses();
442 foreach (Process p in ps)
443 {
444 if (p.ProcessName.ToLower().Equals("excel"))
445 {
446 //if (p.Id == ExcelID)
447 {
448 p.Kill();
449 }
450 }
451 }
452 }
453 catch (Exception ex)
454 {
455 //MessageBox.Show("ERROR " + ex.Message);
456 }
457 }
458
459 #endregion
460
461 #region Events
462
463 #endregion
464
465
466 #region IDisposable 成員
467
468
469 #endregion
470 }
471 }
作者 SamWang