返回顶部

收藏

C#通过com生成Excel文件

更多
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(); 
   } 
  } 

标签:Excel,C#

收藏

0人收藏

支持

0

反对

0

发表评论