C # export Excel,

Source: Internet
Author: User

C # export Excel,

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 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 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)

In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

Related Article

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.