新微赢技术网

标题: .net 生成excel [打印本页]

作者: 渴望光明    时间: 2009-11-30 23:51
标题: .net 生成excel
使用Excel提供的Com对象。
具体的代码如下:
public static void CreateExcel()
   {
    for(int i = 0 ; i<1 ; i++)
    {
     string staFile = "D:\\test\\x";
     System.Reflection.Missing miss = System.Reflection.Missing.Value;
     Excel.ApplicationClass m_objExcel = new Excel.ApplicationClass();
     m_objExcel.Visible = false;
     Excel.Workbooks m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
     Excel.Workbook m_objBook = (Excel.Workbook)(m_objBooks.Add(miss));
     Excel.Worksheet m_objSheet = (Excel.Worksheet)m_objBook.ActiveSheet;
   
     //向Excel文件中写入数据
     Excel.Range er = m_objSheet.get_Range((object)"A1",System.Reflection.Missing.Value);
     er.Value2 = "dfadfa";
     m_objBook.SaveAs(staFile+i.ToString()+".xls", miss, miss, miss, miss,miss, Excel.XlSaveAsAccessMode.xlNoChange, miss,miss,miss, miss, miss);
   
     m_objBook.Close(false, miss, miss);
     m_objBooks.Close();
     m_objExcel.Quit();
     System.Runtime.InteropServices.Marshal.ReleaseComObject(er);
     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
     GC.Collect();
    }
   }

网址大全,网址导航,网址导航大全,网址站,网址之家,最好的网址大全,最好的网址导航




欢迎光临 新微赢技术网 (http://bbs.weiying.cn/) Powered by Discuz! X3.2