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
- Using System;
- Using System. Drawing;
- Using System. Collections;
- Using System. ComponentModel;
- Using System. Data;
- Using System. IO;
- Using System. Text;
- Using System. Windows;
- Using System. Reflection;
- Using System. Runtime. InteropServices;
- Using Excel = Microsoft. Office. Interop. Excel;
Create a new Excel File
(Code snippet 1)
C # code
- Object missing = Missing. Value;
- Excel. Application m_objExcel = new Excel. Application ();
- Excel. Workbooks m_objWorkBooks = m_objExcel.Workbooks;
- Excel. Workbook m_objWorkBook = m_objWorkBooks.Add (true );
- Excel. Sheets m_objWorkSheets = m_objWorkBook.Sheets ;;
- Excel. Worksheet m_objWorkSheet = (Excel. Worksheet) m_objWorkSheets [1];
- Try
- {
- M_objExcel.Save ("f: 129.xls ");
- }
- Catch (Exception e)
- {
- }
- Finally {
- M_objWorkBooks.Close ();
- M_objExcel.Quit ();
- }
------ Operate the cell to navigate by horizontal and vertical coordinates
Add the following code before try {} catch () {}
(Code snippet 2)
C # code
- 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
- M_objExcel.Cells [1, 1] = "Building Energy conservation evaluation and calculation report ";
- Excel. Range range = m_objExcel.get_Range (m_objExcel.Cells [1, 1], m_objExcel.Cells [1, 10]);
- Range. Merge (Type. Missing); // Merge Cells
--- Operation font size, color, cell background color. Border color (Add the following code before try {} catch)
Java code
- M_objExcel.Cells [1, 1] = "Building Energy conservation evaluation and calculation report ";
- Excel. Range range = m_objExcel.get_Range (m_objExcel.Cells [1, 1], m_objExcel.Cells [1, 10]);
- Range. Merge (Type. Missing); // Merge Cells
- Range. Font. Size = 25; // Font Size
- Range. HorizontalAlignment = Excel. Constants. xlCenter; // align
- Range. Font. Bold = 17; // Font size
- // Range. Borders. LineStyle = Excel. XlLineStyle. xlContinuous; // border
- // Range. Borders. Color = ColorTranslator. ToOle (Color. Red); // border Color
- // Range. Interior. ColorIndex = 34; background color
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.