Use. NET to generate Excel files

Source: Internet
Author: User
Tags reflection
Excel| Build Excel
I have encountered some problems with Excel in my work.
On the network to find a lot of methods, each has its pros and cons.
Finally, you chose to use Office Excel Automation services to build Excel files by using the COM objects that Excel provides.
The specific code is as follows:
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;

Writing data to an Excel file
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 ();
}
}
A similar approach has been seen on the web, but they all have a problem that cannot be released after using Excel, and after research I have found that when using COM objects, you must release every instance object that you set up, which is the red part of the code.
Note: Use this code to make your own reference to the Excel component in your project. Another: I only did tests under Office 2003 to make sure that the objects could be freed. In other systems did not do the corresponding test, please study on their own.
Software environment:. NET Framework 1.1 vs.net 2003 OFFICE2003 Windows2000 SP4
Hardware environment: P4 2.4G 512M

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.