C # export Excel

Source: Internet
Author: User

C # For Beginners, because the project requires Excel operations. I searched the internet and found a lot of materials. However, the writing is too redundant, and it is a big headache to cut off the code,

Summary: I forgot to add the deficiencies.

Considering compatibility issues, for office2003 operations

--- Required library Microsoft. Office. InterOP. Excel. dll (Attachment)

---- Classes used in the code

C # code  
  1. Using system;
  2. Using system. drawing;
  3. Using system. collections;
  4. Using system. componentmodel;
  5. Using system. Data;
  6. Using system. IO;
  7. Using system. text;
  8. Using system. windows;
  9. Using system. reflection;
  10. Using system. runtime. interopservices;
  11. Using Excel = Microsoft. Office. InterOP. Excel;

 

Create a new Excel File

 

(Code snippet 1)

C # code  
  1. Object missing = missing. value;
  2. Excel. Application m_objexcel = new excel. Application ();
  3. Excel. workbooks m_objworkbooks = m_objexcel.workbooks;
  4. Excel. Workbook m_objworkbook = m_objworkbooks.add (true );
  5. Excel. Sheets m_objworksheets = m_objworkbook.sheets ;;
  6. Excel. worksheet m_objworksheet = (Excel. worksheet) m_objworksheets [1];
  7. Try
  8. {
  9. M_objexcel.save ("F: 129.xls ");
  10. }
  11. Catch (exception E)
  12. {
  13. }
  14. Finally {
  15. M_objworkbooks.close ();
  16. M_objexcel.quit ();
  17. }

------ Operate the cell to navigate by horizontal and vertical coordinates

Add the following code before try {} catch () {}

(Code snippet 2)

C # code  
  1. M_objexcel.cells [1, 1] = "Building Energy evaluation and calculation report"; // value assignment in the first column of the first line

------ Merge cells (Add the following code before try {} catch)

 

C # code  
  1. M_objexcel.cells [1, 1] = "Building Energy conservation evaluation and calculation report ";
  2. Excel. Range = m_objexcel.get_range (m_objexcel.cells [1, 1], m_objexcel.cells [1, 10]);
  3. Range. Merge (type. Missing); // merge Cells

--- Operation font size, color, cell background color. Border color (Add the following code before try {} catch)

 

 

 

Java code  
  1. M_objexcel.cells [1, 1] = "Building Energy conservation evaluation and calculation report ";
  2. Excel. Range = m_objexcel.get_range (m_objexcel.cells [1, 1], m_objexcel.cells [1, 10]);
  3. Range. Merge (type. Missing); // merge Cells
  4. Range. Font. size = 25; // font size
  5. Range. horizontalalignment = excel. constants. xlcenter; // align
  6. Range. Font. Bold = 17; // font size
  7. // Range. Borders. linestyle = excel. xllinestyle. xlcontinuous; // border
  8. // Range. Borders. Color = colortranslator. Toole (color. Red); // border color
  9. // Range. Interior. colorindex = 34; background color

 

 

  • Dll.rar (166.8 KB)

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.