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;
  }

 

方法2:应该讲这个方法更适合于批量处理

                con.Close();

 

http://blog.csdn.net/lluiss/archive/2004/08/29/88341.aspx

《HOW TO: Transfer Data to an Excel Workbook by Using Visual C# .NET》描述了多种方式(如数组、数据集、ADO.NET、XML)把数据导到Excel表格的方法。

如果你需要把大数据量倒入到Excel 表的话,建议使用 ClipBoard(剪贴板)的方法。实现方法参看上面的连接,讨论参看:http://expert.csdn.net/Expert/topic/3086/3086690.xml 

倒完数据后,在程序退出之前,如果需要结束Excel 的进程,讨论参看:http://expert.csdn.net/Expert/topic/3068/3068466.xml
讨论的结果就是:提前垃圾回收,或者杀死进程。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2021-07-02
猜你喜欢
  • 2021-06-19
  • 2022-12-23
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
相关资源
相似解决方案