excel C# 报表制作汇总

1. 单击“添加引用”对话框的“COM”选项卡,找到“Microsoft Excel 11 Object Library”,按“确定”。
注意:Microsoft Excel 11 Object Library中Excel 11 的版本与安装的OFFICE版本有关。
在编程之前还需要引入命名空间using Microsoft.Office.Interop.Excel;
注意:有的机器上需要用using Excel;我猜这也跟OFFICE版本有关;
这样C#就可以正确识别Excel对象了。    


大体过程是:

Excel.Application app = new Excel.ApplicationClass()
Excel.Workbook wb = null;
Excel.Worksheet ws = null;
Excel.Range range = null;
string filepath = null;
wb = app.Workbooks.Open()
ws = (Excel.Worksheet)wb.Worksheet[sheetname];
range = (Excel.Range)ws.Cells[4,3];
string result = range.Value2.ToString();

range.Value2 = "hello"
range.EntireColumn.AutoFit();
封装代码如下:


2. using System;  
3. using System.Collections.Generic;  
4. using System.Text;  
5. using System.Reflection;  
6. using Microsoft.Office.Core;  
7. using Microsoft.Office.Interop.Excel;  
8. using System.IO;  
9.   
10. namespace ExcelX.ExcelLib  
11. {  
12.     /// <summary>  
13.     /// Excel 操作代理  
14.     /// </summary>  
15.     public class ExcelAgent  
16.     {  
17.         private ApplicationClass _app = null;  
18.         private _Workbook _wb = null;  
19.         private _Worksheet _ws = null;  
20.         private string _filePath = "";  
21.         private int _shIndex = 0; // 1 based index  
22.   
23.         public event EventHandler ExcelExceptionOccured;  
24.   
25.         /// < summary>  
26.         /// 当前Sheet  
27.         /// < /summary>  
28.         public int SheetIndex { get { return this._shIndex; } }  
29.   
30.         /// < summary>  
31.         /// 当前文件名  
32.         /// < /summary>  
33.         public string FileName { get { return this._filePath; } }  
34.   
35.         #region private operations   
36.         /// < summary>  
37.         /// 打开App  
38.         /// < /summary>  
39.         private void OpenApp()  
40.         {  
41.             this._app = new ApplicationClass();  
42.             this._app.Visible = false;  
43.         }  
44.   
45.         /// < summary>  
46.         /// 释放资源  
47.         /// < /summary>  
48.         /// <param name="o"></param>  
49.         private void ReleaseCom(object o)  
50.         {  
51.             try  
52.             {  
53.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(o);//强制释放一个对象 
54.             }  
55.             catch { }  
56.             finally  
57.             {  
58.                 o = null;  
59.             }  
60.         }  
61.   
62.         /// < summary>  
63.         /// 检查App  
64.         /// < /summary>  
65.         private bool CheckApp()  
66.         {  
67.             if (this._app == null)  
68.             {  
69.                 if (this.ExcelExceptionOccured != null)  
70.                 {  
71.                     this.ExcelExceptionOccured(this, new ErrorEventArgs(new Exception("Application对象未初始化"))); 
72.                 }  
73.                 return false;  
74.             }  
75.   
76.             return true;  
77.         }  
78.   
79.         /// < summary>  
80.         /// 检查Book  
81.         /// < /summary>  
82.         private bool CheckWorkBook()  
83.         {  
84.             if (this._wb == null)  
85.             {  
86.                 if (this.ExcelExceptionOccured != null)  
87.                 {  
88.                     this.ExcelExceptionOccured(this, new ErrorEventArgs(new Exception("Workbook对象未初始化"))); 
89.                 }  
90.   
91.                 return false;  
92.             }  
93.   
94.             return true;  
95.         }  
96.   
97.         /// < summary>  
98.         /// 检查Sheet  
99.         /// < /summary>  
100.         private bool CheckSheet()  
101.         {  
102.             if (this._ws == null)  
103.             {  
104.                 if (this.ExcelExceptionOccured != null)  
105.                 {  
106.                     this.ExcelExceptionOccured(this, new ErrorEventArgs(new Exception("Worksheet对象未初始化"))); 
107.                 }  
108.   
109.                 return false;  
110.             }  
111.   
112.             return true;  
113.         }  
114.         #endregion  
115.   
116.         #region basic operation  
117.         /// < summary>  
118.         /// 打开文件  
119.         /// < /summary>  
120.         /// <param name="filePath"></param>  
121.         public void Open(string filePath)  
122.         {  
123.             // Check Application   
124.             if (!this.CheckApp()) return;  
125.   
126.             // Open workbook  
127.             this._filePath = filePath;  
128.             this._wb = this._app.Workbooks._Open(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value 
129.                     , Missing.Value, Missing.Value, Missing.Value, Missing.Value 
130.                     , Missing.Value, Missing.Value, Missing.Value, Missing.Value); 
131.   
132.             // set default sheet  
133.             this.SetCurrentSheet(1);  
134.         }  
135.   
136.         /// <summary>  
137.         /// 自动打开Excel对象  
138.         /// < /summary>  
139.         public ExcelAgent()  
140.         {  
141.             this.OpenApp();  
142.         }  
143.   
144.         /// < summary>  
145.         /// 打开excel文件  
146.         /// < /summary>  
147.         /// <param name="filePath"></param>  
148.         public ExcelAgent(string filePath)  
149.         {  
150.             this.OpenApp();  
151.             this.Open(filePath);  
152.         }  
153.   
154.         /// <summary>  
155.         /// 保存当前文档  
156.         /// < /summary>  
157.         public void Save()  
158.         {  
159.             // check workbook   
160.             if (!this.CheckWorkBook()) return;  
161.   
162.             // save the book  
163.             this._wb.Save();  
164.               
165.         }  
166.   
167.         /// <summary>  
168.         /// 另存当前文档  
169.         /// < /summary>  
170.         /// <param name="filePath"></param>  
171.         public void Save(string filePath)  
172.         {  
173.             // check workbook   
174.             if (!this.CheckWorkBook()) return;  
175.   
176.             // save work book  
177.             this._filePath = filePath;  
178.             bool b = this._app.DisplayAlerts;  
179.             this._app.DisplayAlerts = false;  
180.   
181.             // save work book              
182.             this._wb.SaveAs(this._filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, 
183.                     Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); 
184.   
185.             this._app.DisplayAlerts = b;  
186.         }  
187.   
188.         /// <summary>  
189.         /// 关闭当前操作  
190.         /// < /summary>  
191.         public void Close()  
192.         {  
193.             if (this._app == null) return;  
194.             if (this._wb != null)  
195.             {  
196.                 this._wb.Close(false, Missing.Value, Missing.Value);  
197.                 ReleaseCom(this._wb);  
198.                 this._wb = null;  
199.             }  
200.             this._app.Quit();  
201.             ReleaseCom(this._app);  
202.             this._app = null;  
203.   
204.         }  
205.   
206.         /// <summary>  
207.         /// 设置当前工作Sheet(序号:从1记起)  
208.         /// < /summary>  
209.         /// <param name="sheetIndex"></param>  
210.         public void SetCurrentSheet(int sheetIndex)  
211.         {  
212.             // check workbook   
213.             if (!this.CheckWorkBook()) return;  
214.   
215.             // set sheet object  
216.             this._shIndex = sheetIndex;  
217.             this._ws = (_Worksheet)this._wb.Worksheets[sheetIndex];  
218.         }  
219.   
220.         /// <summary>  
221.         /// 设置当前工作Sheet(序号:从1记起)  
222.         /// < /summary>  
223.         /// <param name="sheetIndex"></param>  
224.         public void SetCurrentSheet(string SheetName)  
225.         {  
226.             // check workbook   
227.             if (!this.CheckWorkBook()) return;  
228.   
229.             // set sheet object  
230.             this._ws = (_Worksheet)this._wb.Worksheets[SheetName];  
231.             this._shIndex = this._ws.Index;  
232.         }  
233.   
234.         /// <summary>  
235.         /// 删除一个工作表  
236.         /// < /summary>  
237.         /// <param name="SheetName"></param>  
238.         public void DeleteSheet()  
239.         {  
240.             // check workbook   
241.             if (!this.CheckSheet()) return;  
242.   
243.             this._ws.Delete();  
244.         }  
245.   
246.         /// <summary>  
247.         /// 改名  
248.         /// < /summary>  
249.         /// <param name="newName"></param>  
250.         public void RenameSheet(string newName)  
251.         {  
252.             // check workbook   
253.             if (!this.CheckSheet()) return;  
254.   
255.             this._ws.Name = newName;  
256.         }  
257.   
258.         /// <summary>  
259.         /// 创建Sheet  
260.         /// < /summary>  
261.         /// <param name="newName"></param>  
262.         public void CreateSheet(string newName)  
263.         {  
264.             // check workbook   
265.             if (!this.CheckWorkBook()) return;  
266.   
267.             this._wb.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value); 
268.         }  
269.   
270.         /// < summary>  
271.         /// 获取数量  
272.         /// < /summary>  
273.         /// <returns></returns>  
274.         public int GetSheetCount()  
275.         {  
276.             // check workbook   
277.             if (!this.CheckWorkBook()) return -1;  
278.   
279.             return this._wb.Worksheets.Count;  
280.         }  
281.         #endregion  
282.   
283.         #region sheet operation  
284.         /// < summary>  
285.         /// 设置单元值  
286.         /// < /summary>  
287.         /// <param name="x"></param>  
288.         /// <param name="y"></param>  
289.         /// <param name="value"></param>  
290.         public void SetCellValue(int x, int y, object value)  
291.         {  
292.             if (!this.CheckSheet()) return;  
293.             this._ws.Cells[x, y] = value;  
294.         }  
295.   
296.         /// <summary>  
297.         /// 合并单元格  
298.         /// < /summary>  
299.         /// <param name="x1"></param>  
300.         /// <param name="y1"></param>  
301.         /// <param name="x2"></param>  
302.         /// <param name="y2"></param>  
303.         public void UniteCells(int x1, int y1, int x2, int y2)  
304.         {  
305.             if (!this.CheckSheet()) return;  
306.             this._ws.get_Range(this._ws.Cells[x1, y1], this._ws.Cells[x2, y2]).Merge(Type.Missing); 
307.         }  
308.   
309.         /// <summary>  
310.         /// 将内存中数据表格插入到Excel指定工作表的指定位置 为在使用模板时控制格式时使用一  
311.         /// < /summary>  
312.         /// <param name="dt"></param>  
313.         /// <param name="startX"></param>  
314.         /// <param name="startY"></param>  
315.         public void InsertTable(System.Data.DataTable dt, int startX, int startY) 
316.         {  
317.             if (!this.CheckSheet()) return;  
318.   
319.             for (int i = 0; i <= dt.Rows.Count - 1; i++)  
320.             {  
321.                 for (int j = 0; j <= dt.Columns.Count - 1; j++)  
322.                 {  
323.                     this._ws.Cells[startX + i, j + startY] = dt.Rows[i][j].ToString(); 
324.   
325.                 }  
326.   
327.             }  
328.   
329.         }  
330.   
331.         /// <summary>  
332.         /// 获取单元格值  
333.         /// < /summary>  
334.         /// <param name="cellName"></param>  
335.         /// < returns></returns>  
336.         public object GetCellValue(string cellName)  
337.         {  
338.             if (!this.CheckSheet()) return null;  
339.   
340.             Range range = this._ws.get_Range(cellName, Type.Missing);  
341.   
342.             return range.Value2;  
343.         }  
344.   
345.         /// <summary>  
346.         /// 获取单元格值  
347.         /// < /summary>  
348.         /// <param name="row"></param>  
349.         /// <param name="col"></param>  
350.         /// < returns></returns>  
351.         public object GetCellValue(int row, int col)  
352.         {  
353.             if (!this.CheckSheet()) return null;  
354.   
355.             Range range = (Range)this._ws.Cells[row, col];  
356.   
357.             return range.Value2;  
358.         }  
359.   
360.         public string GetStringValue(string cellName)  
361.         {  
362.             object val = this.GetCellValue(cellName);  
363.             string result = "";  
364.   
365.             if (val != null) result = val.ToString();  
366.   
367.             return result;  
368.         }  
369.   
370.         public string GetStringValue(int row, int col)  
371.         {  
372.             object val = this.GetCellValue(row, col);  
373.             string result = "";  
374.   
375.             if (val != null) result = val.ToString();  
376.   
377.             return result;  
378.         }  
379.   
380.         public double GetDoubleValue(string cellName)  
381.         {  
382.             object val = this.GetCellValue(cellName);  
383.             string result = "";  
384.   
385.             if (val != null) result = val.ToString();  
386.   
387.             double number = 0d;  
388.             if (double.TryParse(result, out number))  
389.             {  
390.                 number = double.Parse(result);  
391.             }  
392.             else  
393.             {  
394.                 number = 0d;  
395.             }  
396.   
397.             return number;  
398.         }  
399.   
400.         public double GetDoubleValue(int row, int col)  
401.         {  
402.             object val = this.GetCellValue(row, col);  
403.             string result = "";  
404.   
405.             if (val != null) result = val.ToString();  
406.   
407.             double number = 0d;  
408.             if (double.TryParse(result, out number))  
409.             {  
410.                 number = double.Parse(result);  
411.             }  
412.             else  
413.             {  
414.                 number = 0d;  
415.             }  
416.   
417.             return number;  
418.         }  
419.          
420.         #endregion  
421.     }  
422. }


要:本文介绍了Excel对象、C#中的受管代码和非受管代码,并介绍了COM组件在.NET环境中的使用。
关键词:受管代码;非受管代码;Excel对象;动态连接库


0 引言
Excel是微软公司办公自动化套件中的一个软件,他主要是用来处理电子表格。Excel以其功能强大,界面友好等受到了许多用户的欢迎。在设计应用系统时,对于不同的用户,他们对于打印的需求是不一样的,如果要使得程序中的打印功能适用于每一个用户,可以想象程序设计是十分复杂的。由于Excel表格的功能强大,又由于几乎每一台机器都安装了它,如果把程序处理的结果放到Excel表格中,这样每一个用户就可以根据自己的需要在Excel中定制自己的打印。这样不仅使得程序设计简单,而且又满足了诸多用户的要求,更加实用了。那么用Visual C#如何调用Excel,如何又把数据存放到Excel表格中?本文就来探讨上述问题的解决办法。
1 Excel对象
  微软的Excel对象模型包括了128个不同的对象,从矩形,文本框等简单的对象到透视表,图表等复杂的对象.下面我们简单介绍一下其中最重要,也是用得最多的四个对象。
(1) Application对象。Application对象处于Excel对象层次结构的顶层,表示Excel自身的运行环境。 (2) Workbook对象。Workbook对象直接地处于Application对象的下层,表示一个Excel工作薄文件。
(3) Worksheet对象。Worksheet对象包含于Workbook对象,表示一个Excel工作表。
(4) Range对象。Range对象包含于Worksheet对象,表示Excel工作表中的一个或多个单元格。
2 C#中的受管代码和非受管代码 
在.NET公用语言框架内运行的程序为受管代码。受管代码在程序中所有类型都受到严格检查,没有指针,对内存的管理完全由运行系统控制。受控状态下,编写程序更为容易,且更少出错,我们可以花更多的时间在解决实际问题上而不是在计算机语言问题上。相对而言,那些在.NET框架外运行的程序为非受管代码。比如:COM组件、ActiveX组件、Win32 API函数、指针运算等。C#编程中在某些特定情况下,需要运用非受管代码,例如,要利用一个成熟的COM组件,或者调用一个API函数,或者用指针去编写实时/高效程序等。
3 Visual C#中调用Excel的COM组件
一个.NET组件事实上是一个.NET下的DLL,它包含的不仅是运行程序本身,更重要的是包含这个DLL的描述信息(Meta Data,即元数据),而一个COM组件是用其类库(TLB)储存其描述信息。这些COM组件都是非受管代码,要在Visual C#中使用这些非受管代码的COM组件,就必须把他们转换成受管代码的.NET组件。所以在用Visual C#调用Excel表格之前,必须完成从COM组件的非受管代码到受管代码的类库的转换。
3.1 将Excel的COM组件转换为.NET组件
在项目中打开Add Reference对话框,选择COM栏,之后在COM列表中找到“Microsoft Excel 9.0 Object Library”(Office 2000),然后将其加入到项目的References中即可。Visual C#.NET会自动产生相应的.NET组件文件,以后即可正常使用。
这个转换形成.NET组件不能单独使用,它不过是以前的COM组件的一个外层包装,在.NET中可以通过这个外层包装去发现原来的COM组件并调用其相应的界面函数。所以它必须与原来的COM组件一起起作用。
3.2 Visual C#打开Excel表格
事实上,在C#中使用一个经转换的COM组件和使用任何一个其它.NET组件完全一样。可以用new关键字创建一个经转换的COM组件,然后再像使用任何一个其它C#对象一样使用这个组件对象。
在转换后的.NET组件中定义了一个命名空间Excel,在此命名空间中封装了一个类Application,这个类和启动Excel表格有非常重要的关系,在Visual C#中,只需要下列三行代码就可以完成打开Excel表格的工作,具体如下:


Excel.Application excel = new Excel.Application ();//引用Excel对象
excel.Application.Workbooks.Add ( true );//引用Excel工作簿
excel.Visible = true ;//使Excel可视


  但此时的Excel表格是一个空的表格,没有任何内容,下面就来介绍如何往Excel表格中输入数据。
3.3 往Excel表格中输入数据
  在命名空间"Excel"中,还定义了一个类"Cell",这个类所代表的就是Excel表格中的一个单元格。通过给"Cell"赋值,从而实现往Excel表格中输入相应的数据,下列代码功能是打开Excel表格,并且往表格输入一些数据。
Excel.Application excel = new Excel.Application () ;
excel.Application.Workbooks.Add ( true ) ;
excel.Cells[ 1 , 1 ] = "First Row First Column" ;
excel.Cells[ 1 , 2 ] = "First Row Second Column" ;
excel.Cells[ 2 , 1 ] = "Second Row First Column" ;
excel.Cells[ 2 , 2 ] = "Second Row Second Column" ;
excel.Visible = true ; 
3.4 实例
下面实例在C#中连接Oracle数据库(Name),从表(TableName)中读取数据,并写入Excel.
string cnString="Provider=msdaora.1;Data source=Name; ";
cnString=cnString+"user id=UserName;password=Password";
try
{
OleDbConnection cn=new OleDbConnection (cnString);
cn.Open ();
try
{
string s="select * from Name.TableName";
OleDbCommand cmd=new OleDbCommand (s,cn);
OleDbDataReader dr=cmd.ExecuteReader ();
Excel.Application xlApp = new Excel.Application();
if(xlApp==null){MessageBox.Show ("Can’t open Excel!");return;}
xlApp.Application .Workbooks .Add (true);
int row=2,fieldcount;
fieldcount=dr.FieldCount ;
for(int col=0;col<fieldcount;col++) xlApp.Cells [1,col+1]=dr.GetName(col);
while (dr.Read ())
{
for(int col=0;col<fieldcount;col++) 
xlApp.Cells [row,col+1]=dr.GetValue(col).ToString();
row++;
}
xlApp.Visible =true;
xlApp=null;
}
catch(Exception ex ){MessageBox.Show (ex.Message );}
finally {cn.Close();}
}
catch(Exception ex){MessageBox.Show (ex.Message );}
}
}
3.5安装一个使用COM组件的.NET程序
如果要将这样的程序安装运行在另一台机器上,那么除了安装运行程序外,还做三件事。
首先,是安装.NET运行系统。因为任何一个.NET程序都不能离开.NET运行系统去独立运行。
其次,所调用的COM组件必须要安装在目标机器上。本例中大多数目标机器上都装有Microsoft Office的Excel,一般不会有这个问题。但如果是另一个用户自定义的COM组件,那么这个COM组件在运行.NET程序之前必须先安装好。
最后,转换后的.NET组件DLL文件要安装在目标机器上。因为.NET组件不需要在Windows Registry中注册,所以最简单的方法是将.NET组件DLL文件拷贝到运行程序目录下。如果此.NET组件被多个.NET程序共享,可以将其安装在.NET公用组件区中,从而可被任何一个.NET组件使用。只有当一个.NET组件参与了事务处理时,才需要将它注册为一个COM+组件。因为.NET仍然用传统的COM+机制来处理事务的提交、回滚等。
4 小结
通过以上讨论,我们知道了在C#中,如何使用Excel的COM组件。需要注意的是,Excel对象包含的许多内容我们没有介绍,在使用过程中需要我们不断学习。也使我们了解了在C#中如何使用COM组件。


参考文献:
[1] 刘洪成 C#高级编程 清华大学出版社 2003.7工作 187~200


C#中创建、打开、读取、写入、保存Excel的一般性代码


首先,在引用的COM中找到Microsoft Excel 11.0 Object Library,添加。


using System; 
using System.Reflection; // 引用这个才能使用Missing字段 
using Excel;




namespace CExcel1 

  class Class1 
  { 
    [STAThread] 
    static void Main(string[] args) 
    { 
      //创建Application对象 
      Excel.Application xApp=new Excel.ApplicationClass(); 


      xApp.Visible=true; 
      //得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件 
      Excel.Workbook xBook=xApp.Workbooks._Open(@"D:\Sample.xls", 
        Missing.Value,Missing.Value,Missing.Value,Missing.Value 
        ,Missing.Value,Missing.Value,Missing.Value,Missing.Value 
        ,Missing.Value,Missing.Value,Missing.Value,Missing.Value);         


      //xBook=xApp.Workbooks.Add(Missing.Value);//新建文件的代码 
      //指定要操作的Sheet,两种方式: 


      Excel.Worksheet xSheet=(Excel.Worksheet)xBook.Sheets[1]; 
      //Excel.Worksheet xSheet=(Excel.Worksheet)xApp.ActiveSheet; 


      //读取数据,通过Range对象 
      Excel.Range rng1=xSheet.get_Range("A1",Type.Missing); 
      Console.WriteLine(rng1.Value2); 


      //读取,通过Range对象,但使用不同的接口得到Range 
      Excel.Range rng2=(Excel.Range)xSheet.Cells[3,1]; 
      Console.WriteLine(rng2.Value2); 


      //写入数据 
      Excel.Range rng3=xSheet.get_Range("C6",Missing.Value); 
      rng3.Value2="Hello"; 
      rng3.Interior.ColorIndex=6; //设置Range的背景色 


      //保存方式一:保存WorkBook 
      xBook.SaveAs(@"D:\CData.xls", 
       Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value, 
       Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,
       Missing.Value,Missing.Value); 


      //保存方式二:保存WorkSheet 
      xSheet.SaveAs(@"D:\CData2.xls", 
       Missing.Value,Missing.Value,Missing.Value,Missing.Value, 
       Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value); 


      //保存方式三 
      xBook.Save(); 
      xSheet=null; 
      xBook=null; 
      xApp.Quit(); //这一句是非常重要的,否则Excel对象不能从内存中退出 
      xApp=null; 
    } 
  } 
}


C#如何向EXCEL写入数据


我按着微软技术支持网上的方法写入数据:使用“自动化”功能逐单元格传输数据,代码如下:
// Start a new workbook in Excel.
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));


// Add data to cells in the first worksheet in the new workbook.
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
m_objRange = m_objSheet.get_Range("A1", m_objOpt);
m_objRange.Value = "Last Name";
m_objRange = m_objSheet.get_Range("B1", m_objOpt);
m_objRange.Value = "First Name";
m_objRange = m_objSheet.get_Range("A2", m_objOpt);
m_objRange.Value = "Doe";
m_objRange = m_objSheet.get_Range("B2", m_objOpt);
m_objRange.Value = "John";


// Apply bold to cells A1:B1.
m_objRange = m_objSheet.get_Range("A1", "B1");
m_objFont = m_objRange.Font;
m_objFont.Bold=true;


// Save the Workbook and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book1.xls", m_objOpt, m_objOpt, 
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, 
m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();


EXCEL表用c#来读


using System;
using System.IO;
using System.Web;
using System.Web.SessionState;
using NickLee.Common.ExcelLite;




namespace excel1
{
    /// <summary>
    /// excel 的摘要说明。
    /// </summary>
    public class excel
    {
    
        public excel()
        {                        
            //
            // TODO: 在此处添加构造函数逻辑
            //
            fileName =  HttpContext.Current.Server.
        MapPath(".")+"\\temp\\"+g+".xls";
        }
        
        private     Guid g = Guid.NewGuid();
        private  string fileName;
        private   ExcelFile ec =new ExcelFile();    
                    
        /// <summary>
        /// 从model中复制模版到temp中
        /// </summary>
        /// <param name="modelName">只需传入模版的名字即可(注意是字符串)</param>
        public void copyModel(string modelName)
        {
            string MName = HttpContext.
       Current.Server.MapPath(".")+"\\model\\"+modelName;
            File.Copy(MName,fileName);
        }
        /// <summary>
        /// 设置EXCEL表中的字表个数,字表在这个表中统一命名为“续表”+数字序号
        /// </summary>
        /// <param name="sheetnum">设置字表的个数</param>
        public  void setSheets(int sheetnum)
        {
            ec.LoadXls(fileName);
            ExcelWorksheet wsx =ec.Worksheets[0];
            for (int y=0;y<sheetnum;y++) 
            {
                int num = y+1;
                 ec.Worksheets.AddCopy("续表"+num,wsx);
            }
      }
        /// <summary>
        /// 给EXCEL表的各个字段设置值
        /// </summary>
        /// <param name="sheetnum">表的序号,注意表是以“0”开始的</param>
        /// <param name="x">对应于EXCEL的最左边的数字列</param>
        /// <param name="y">对应于EXCEL的最上面的字母列</param>
        /// <param name="values">需要写入的值</param>
        public  void setValues(int sheetnum,int x,int y,string values)
        {
            ec.Worksheets[sheetnum].Cells[x,y].Value=values;;
        }
        /// <summary>
        /// 保存函数,要是文件中有相同的文件就要删除了,
         ///然后重新写入相同文件名的新EXCEL表
        /// </summary>
        public void  saveFile()
        {
            if(File.Exists(fileName))
            {                                
                File.Delete(fileName);
                ec.SaveXls(fileName);
            }
            else
            {
                ec.SaveXls(fileName);
            }
         
        }
        /// <summary>
        /// 显示的EXCEL表格
        /// </summary>
        public void showFile()
        {
            HttpContext.Current.Response.Charset = 
                        System.Text.Encoding.Default.WebName; 
            HttpContext.Current.Response.ContentType =
 "application/vnd.ms-excel";
            HttpContext.Current.Response.ContentEncoding =
 System.Text.Encoding.UTF8;  
            HttpContext.Current.Response.WriteFile(fileName);
            HttpContext.Current.Response.End();
        }
    


    }
}
 
 
 C#读写Excel文档(---续C#读写Word文件)
距离上次写那篇《也说C#实现对Word文件读写》已经一年多时间了,一直想小结一些C#读写Excel文件的相关技巧,毕竟Excel打印更为方便和实用,一个是Excel打印输出编码比Word文件打印数据简单些,另一个是Excel本身对数据超强计算处理功能;赶巧最近项目又涉及Excel报表统计打印的问题,所以在把其中的一些技术记录下来与大家一起分析讨论,次篇主要涉及两个方面内容:
1、 读写Excel文件
A、设计Excel模版
B、打开一个目标文件并且读取模版内容
C、目标文件按格式写入需要的数据
D、保存并且输出目标Excel文件 
2、 Excel对象资源释放,这个在以前项目没有注意彻底释放使用到Excel对象,对客户计算机资源造成一定浪费,此次得到彻底解决。
 
  
下面是一个Excel打印输出的Demo 
1、 创建一个叫DemoExcel的项目
 
2、 引用COM,包括:Microsoft.Excel.x.0.Object.Library,Microsoft.Office.x.0.Object.Library
 
建议安装正版OFFICE,而且版本在11.0以上(Office2003以上),引用以上两个Com后,在项目引用栏发现多了Excel、Microsoft.Office.Core,VBIDE三个 Library.
3、 下面建立一些模拟的数据,此处为街镇信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.IO;
using System.Reflection;


namespace DemoExcel
{
    public partial class Form1 : Form
    {
        private  object missing = Missing.Value;
        private Microsoft.Office.Interop.Excel.Application ExcelRS;
        private Microsoft.Office.Interop.Excel.Workbook RSbook;
        private Microsoft.Office.Interop.Excel.Worksheet RSsheet;


        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“dataSet1.STREET”中。您可以根据需要移动或移除它。
            this.sTREETTableAdapter.Fill(this.dataSet1.STREET);


        }


        private void button1_Click(object sender, EventArgs e)
        {
            string OutFilePath = System.Windows.Forms.Application.StartupPath + @" emp.xls";
           
            string TemplateFilePath = System.Windows.Forms.Application.StartupPath + @"模版.xls";
            PrintInit(TemplateFilePath,OutFilePath);
        }
        //Excle输出前初始化
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool PrintInit(string templetFile, string outputFile)
        {
            try
            {
                if (templetFile == null)
                {
                    MessageBox.Show("Excel模板文件路径不能为空!");
                    return false;
                }
                if (outputFile == null)
                {
                    MessageBox.Show("输出Excel文件路径不能为空!");
                    return false;
                }
                //把模版文件templetFile拷贝到目输出文件outputFile中,并且目标文件可以改写
                System.IO.File.Copy(templetFile, outputFile, true);
                if (this.ExcelRS != null)
                    ExcelRS = null;
                //实例化ExcelRS对象
                ExcelRS = new Microsoft.Office.Interop.Excel.ApplicationClass();
                //打开目标文件outputFile
                RSbook = ExcelRS.Workbooks.Open(outputFile, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);
                //设置第一个工作溥
                RSsheet = (Microsoft.Office.Interop.Excel.Worksheet)RSbook.Sheets.get_Item(1);
                //激活当前工作溥
                RSsheet.Activate();
                
                在当前工作溥写入内容
                for (int i = 0; i < this.dataGridView1.RowCount; i++)
                {
                    RSsheet.Cells[3 + i, 1] = this.dataGridView1[0, i].Value.ToString();
                    RSsheet.Cells[3 + i, 2] = this.dataGridView1[1, i].Value.ToString();
                    RSsheet.Cells[3 + i, 3] = this.dataGridView1[2, i].Value.ToString();
                } 
                


                //保存目标文件
                RSbook.Save();
                //设置DisplayAlerts
                ExcelRS.DisplayAlerts = false;
                ExcelRS.Visible = true;
                //ExcelRS.DisplayAlerts = true;


                //释放对象
                RSsheet = null;
                RSbook = null;
                ExcelRS = null;
                //释放内存
                GcCollect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;
        }


        public void GcCollect()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}
特别说明:


a、引用Microsoft.Office.Interop.Excel;using Microsoft.Office.Core;


b、(关键)在程序中特别释放Excel资源的时候既要设置对象为null,又要强制回收内存,这样才能彻底回收资源。


c、引用的Office组建版本是个敏感问题,不同版本之间有细微差别,需要分别处理。


第二种方法:


1. 单击“添加引用”对话框的“COM”选项卡,找到“Microsoft Excel 11 Object Library”,按“确定”。
注意:Microsoft Excel 11 Object Library中Excel 11 的版本与安装的OFFICE版本有关。
在编程之前还需要引入命名空间using Microsoft.Office.Interop.Excel;
注意:有的机器上需要用using Excel;我猜这也跟OFFICE版本有关;
这样C#就可以正确识别Excel对象了。    


大体过程是:

Excel.Application app = new Excel.ApplicationClass()
Excel.Workbook wb = null;
Excel.Worksheet ws = null;
Excel.Range range = null;
string filepath = null;
wb = app.Workbooks.Open()
ws = (Excel.Worksheet)wb.Worksheet[sheetname];
range = (Excel.Range)ws.Cells[4,3];
string result = range.Value2.ToString();

range.Value2 = "hello"
range.EntireColumn.AutoFit();
封装代码如下:


2. using System;  
3. using System.Collections.Generic;  
4. using System.Text;  
5. using System.Reflection;  
6. using Microsoft.Office.Core;  
7. using Microsoft.Office.Interop.Excel;  
8. using System.IO;  
9.   
10. namespace ExcelX.ExcelLib  
11. {  
12.     /// <summary>  
13.     /// Excel 操作代理  
14.     /// </summary>  
15.     public class ExcelAgent  
16.     {  
17.         private ApplicationClass _app = null;  
18.         private _Workbook _wb = null;  
19.         private _Worksheet _ws = null;  
20.         private string _filePath = "";  
21.         private int _shIndex = 0; // 1 based index  
22.   
23.         public event EventHandler ExcelExceptionOccured;  
24.   
25.         /// <summary>  
26.         /// 当前Sheet  
27.         /// </summary>  
28.         public int SheetIndex { get { return this._shIndex; } }  
29.   
30.         /// <summary>  
31.         /// 当前文件名  
32.         /// </summary>  
33.         public string FileName { get { return this._filePath; } }  
34.   
35.         #region private operations   
36.         /// <summary>  
37.         /// 打开App  
38.         /// </summary>  
39.         private void OpenApp()  
40.         {  
41.             this._app = new ApplicationClass();  
42.             this._app.Visible = false;  
43.         }  
44.   
45.         /// <summary>  
46.         /// 释放资源  
47.         /// </summary>  
48.         /// <param name="o"></param>  
49.         private void ReleaseCom(object o)  
50.         {  
51.             try  
52.             {  
53.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(o);//强制释放一个对象  
54.             }  
55.             catch { }  
56.             finally  
57.             {  
58.                 o = null;  
59.             }  
60.         }  
61.   
62.         /// <summary>  
63.         /// 检查App  
64.         /// </summary>  
65.         private bool CheckApp()  
66.         {  
67.             if (this._app == null)  
68.             {  
69.                 if (this.ExcelExceptionOccured != null)  
70.                 {  
71.                     this.ExcelExceptionOccured(this, new ErrorEventArgs(new Exception("Application对象未初始化")));  
72.                 }  
73.                 return false;  
74.             }  
75.   
76.             return true;  
77.         }  
78.   
79.         /// <summary>  
80.         /// 检查Book  
81.         /// </summary>  
82.         private bool CheckWorkBook()  
83.         {  
84.             if (this._wb == null)  
85.             {  
86.                 if (this.ExcelExceptionOccured != null)  
87.                 {  
88.                     this.ExcelExceptionOccured(this, new ErrorEventArgs(new Exception("Workbook对象未初始化")));  
89.                 }  
90.   
91.                 return false;  
92.             }  
93.   
94.             return true;  
95.         }  
96.   
97.         /// <summary>  
98.         /// 检查Sheet  
99.         /// </summary>  
100.         private bool CheckSheet()  
101.         {  
102.             if (this._ws == null)  
103.             {  
104.                 if (this.ExcelExceptionOccured != null)  
105.                 {  
106.                     this.ExcelExceptionOccured(this, new ErrorEventArgs(new Exception("Worksheet对象未初始化")));  
107.                 }  
108.   
109.                 return false;  
110.             }  
111.   
112.             return true;  
113.         }  
114.         #endregion  
115.   
116.         #region basic operation  
117.         /// <summary>  
118.         /// 打开文件  
119.         /// </summary>  
120.         /// <param name="filePath"></param>  
121.         public void Open(string filePath)  
122.         {  
123.             // Check Application   
124.             if (!this.CheckApp()) return;  
125.   
126.             // Open workbook  
127.             this._filePath = filePath;  
128.             this._wb = this._app.Workbooks._Open(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value  
129.                     , Missing.Value, Missing.Value, Missing.Value, Missing.Value  
130.                     , Missing.Value, Missing.Value, Missing.Value, Missing.Value);  
131.   
132.             // set default sheet  
133.             this.SetCurrentSheet(1);  
134.         }  
135.   
136.         /// <summary>  
137.         /// 自动打开Excel对象  
138.         /// </summary>  
139.         public ExcelAgent()  
140.         {  
141.             this.OpenApp();  
142.         }  
143.   
144.         /// <summary>  
145.         /// 打开excel文件  
146.         /// </summary>  
147.         /// <param name="filePath"></param>  
148.         public ExcelAgent(string filePath)  
149.         {  
150.             this.OpenApp();  
151.             this.Open(filePath);  
152.         }  
153.   
154.         /// <summary>  
155.         /// 保存当前文档  
156.         /// </summary>  
157.         public void Save()  
158.         {  
159.             // check workbook   
160.             if (!this.CheckWorkBook()) return;  
161.   
162.             // save the book  
163.             this._wb.Save();  
164.               
165.         }  
166.   
167.         /// <summary>  
168.         /// 另存当前文档  
169.         /// </summary>  
170.         /// <param name="filePath"></param>  
171.         public void Save(string filePath)  
172.         {  
173.             // check workbook   
174.             if (!this.CheckWorkBook()) return;  
175.   
176.             // save work book  
177.             this._filePath = filePath;  
178.             bool b = this._app.DisplayAlerts;  
179.             this._app.DisplayAlerts = false;  
180.   
181.             // save work book              
182.             this._wb.SaveAs(this._filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange,  
183.                     Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);  
184.   
185.             this._app.DisplayAlerts = b;  
186.         }  
187.   
188.         /// <summary>  
189.         /// 关闭当前操作  
190.         /// </summary>  
191.         public void Close()  
192.         {  
193.             if (this._app == null) return;  
194.             if (this._wb != null)  
195.             {  
196.                 this._wb.Close(false, Missing.Value, Missing.Value);  
197.                 ReleaseCom(this._wb);  
198.                 this._wb = null;  
199.             }  
200.             this._app.Quit();  
201.             ReleaseCom(this._app);  
202.             this._app = null;  
203.   
204.         }  
205.   
206.         /// <summary>  
207.         /// 设置当前工作Sheet(序号:从1记起)  
208.         /// </summary>  
209.         /// <param name="sheetIndex"></param>  
210.         public void SetCurrentSheet(int sheetIndex)  
211.         {  
212.             // check workbook   
213.             if (!this.CheckWorkBook()) return;  
214.   
215.             // set sheet object  
216.             this._shIndex = sheetIndex;  
217.             this._ws = (_Worksheet)this._wb.Worksheets[sheetIndex];  
218.         }  
219.   
220.         /// <summary>  
221.         /// 设置当前工作Sheet(序号:从1记起)  
222.         /// </summary>  
223.         /// <param name="sheetIndex"></param>  
224.         public void SetCurrentSheet(string SheetName)  
225.         {  
226.             // check workbook   
227.             if (!this.CheckWorkBook()) return;  
228.   
229.             // set sheet object  
230.             this._ws = (_Worksheet)this._wb.Worksheets[SheetName];  
231.             this._shIndex = this._ws.Index;  
232.         }  
233.   
234.         /// <summary>  
235.         /// 删除一个工作表  
236.         /// </summary>  
237.         /// <param name="SheetName"></param>  
238.         public void DeleteSheet()  
239.         {  
240.             // check workbook   
241.             if (!this.CheckSheet()) return;  
242.   
243.             this._ws.Delete();  
244.         }  
245.   
246.         /// <summary>  
247.         /// 改名  
248.         /// </summary>  
249.         /// <param name="newName"></param>  
250.         public void RenameSheet(string newName)  
251.         {  
252.             // check workbook   
253.             if (!this.CheckSheet()) return;  
254.   
255.             this._ws.Name = newName;  
256.         }  
257.   
258.         /// <summary>  
259.         /// 创建Sheet  
260.         /// </summary>  
261.         /// <param name="newName"></param>  
262.         public void CreateSheet(string newName)  
263.         {  
264.             // check workbook   
265.             if (!this.CheckWorkBook()) return;  
266.   
267.             this._wb.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);  
268.         }  
269.   
270.         /// <summary>  
271.         /// 获取数量  
272.         /// </summary>  
273.         /// <returns></returns>  
274.         public int GetSheetCount()  
275.         {  
276.             // check workbook   
277.             if (!this.CheckWorkBook()) return -1;  
278.   
279.             return this._wb.Worksheets.Count;  
280.         }  
281.         #endregion  
282.   
283.         #region sheet operation  
284.         /// <summary>  
285.         /// 设置单元值  
286.         /// </summary>  
287.         /// <param name="x"></param>  
288.         /// <param name="y"></param>  
289.         /// <param name="value"></param>  
290.         public void SetCellValue(int x, int y, object value)  
291.         {  
292.             if (!this.CheckSheet()) return;  
293.             this._ws.Cells[x, y] = value;  
294.         }  
295.   
296.         /// <summary>  
297.         /// 合并单元格  
298.         /// </summary>  
299.         /// <param name="x1"></param>  
300.         /// <param name="y1"></param>  
301.         /// <param name="x2"></param>  
302.         /// <param name="y2"></param>  
303.         public void UniteCells(int x1, int y1, int x2, int y2)  
304.         {  
305.             if (!this.CheckSheet()) return;  
306.             this._ws.get_Range(this._ws.Cells[x1, y1], this._ws.Cells[x2, y2]).Merge(Type.Missing);  
307.         }  
308.   
309.         /// <summary>  
310.         /// 将内存中数据表格插入到Excel指定工作表的指定位置 为在使用模板时控制格式时使用一  
311.         /// </summary>  
312.         /// <param name="dt"></param>  
313.         /// <param name="startX"></param>  
314.         /// <param name="startY"></param>  
315.         public void InsertTable(System.Data.DataTable dt, int startX, int startY)  
316.         {  
317.             if (!this.CheckSheet()) return;  
318.   
319.             for (int i = 0; i <= dt.Rows.Count - 1; i++)  
320.             {  
321.                 for (int j = 0; j <= dt.Columns.Count - 1; j++)  
322.                 {  
323.                     this._ws.Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();  
324.   
325.                 }  
326.   
327.             }  
328.   
329.         }  
330.   
331.         /// <summary>  
332.         /// 获取单元格值  
333.         /// </summary>  
334.         /// <param name="cellName"></param>  
335.         /// <returns></returns>  
336.         public object GetCellValue(string cellName)  
337.         {  
338.             if (!this.CheckSheet()) return null;  
339.   
340.             Range range = this._ws.get_Range(cellName, Type.Missing);  
341.   
342.             return range.Value2;  
343.         }  
344.   
345.         /// <summary>  
346.         /// 获取单元格值  
347.         /// </summary>  
348.         /// <param name="row"></param>  
349.         /// <param name="col"></param>  
350.         /// <returns></returns>  
351.         public object GetCellValue(int row, int col)  
352.         {  
353.             if (!this.CheckSheet()) return null;  
354.   
355.             Range range = (Range)this._ws.Cells[row, col];  
356.   
357.             return range.Value2;  
358.         }  
359.   
360.         public string GetStringValue(string cellName)  
361.         {  
362.             object val = this.GetCellValue(cellName);  
363.             string result = "";  
364.   
365.             if (val != null) result = val.ToString();  
366.   
367.             return result;  
368.         }  
369.   
370.         public string GetStringValue(int row, int col)  
371.         {  
372.             object val = this.GetCellValue(row, col);  
373.             string result = "";  
374.   
375.             if (val != null) result = val.ToString();  
376.   
377.             return result;  
378.         }  
379.   
380.         public double GetDoubleValue(string cellName)  
381.         {  
382.             object val = this.GetCellValue(cellName);  
383.             string result = "";  
384.   
385.             if (val != null) result = val.ToString();  
386.   
387.             double number = 0d;  
388.             if (double.TryParse(result, out number))  
389.             {  
390.                 number = double.Parse(result);  
391.             }  
392.             else  
393.             {  
394.                 number = 0d;  
395.             }  
396.   
397.             return number;  
398.         }  
399.   
400.         public double GetDoubleValue(int row, int col)  
401.         {  
402.             object val = this.GetCellValue(row, col);  
403.             string result = "";  
404.   
405.             if (val != null) result = val.ToString();  
406.   
407.             double number = 0d;  
408.             if (double.TryParse(result, out number))  
409.             {  
410.                 number = double.Parse(result);  
411.             }  
412.             else  
413.             {  
414.                 number = 0d;  
415.             }  
416.   
417.             return number;  
418.         }  
419.          
420.         #endregion  
421.     }  
422. }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值