SoFunction
Updated on 2025-03-07

C# How to open Excel files in WinForm or open Excel app to display data

1. How to open Excel files in WinForm

step:

1. Add a reference to "Microsoft Excel 16.0 Object Library" and export the namespace:

using ;
using Excel = ;

2. Create an Excel application object, and obtain a Workbook object and a Worksheet object.

 ExcelApp = new ();

 worksBook = (); //Create a new workbook//You can also add an existing workbook// wb = ("D://");

 workSheet = ()[1]; //Get sheet in the workbook.//Can also be used workSheet = ()Statement acquisition

3. Show Excel applications!

 = true;

2. Detailed explanation of the code for opening Excel file

(); method, create a new worksheet. The new worksheet will become an activity worksheet. There is an optional parameter in () and the type is object.

If this parameter is a string that specifies the existing Microsoft Excel file name, creating a new workbook takes the specified file as a template.

If this parameter is a constant, the new workbook will contain a worksheet of the specified type.

This parameter can be one of the following XlWBATemplate constants: xlWBATChart, xlWBATExcel4IntlMacroSheet, xlWBATExcel4MacroSheet, or xlWBATWorksheet.

  • xlWBATChart -4109 Chart.
  • xlWBATExcel4IntlMacroSheet 4 Excel version 4 macro.
  • xlWBATExcel4MacroSheet 3 Excel version 4 International Macro.
  • xlWBATWorksheet -4167 Worksheet.

When opening an Excel file, you can set some workSheet properties, as follows:

 = "Test Sheet1"; //The name of the worksheet = true; //Set all columns' text wrapping automatically(); //Set all columns to automatically adjust row height

3. How to insert data into Excel

After getting the Worksheet object, we can insert data into the table.

The example code is as follows:

[1, "A"].Value = "I'm a string";

The way to get the cell is:

[row, column]; 

Cells is a read-only property in WorkSheet, Range type, used to obtain a certain cell.

If you want to get the value of this cell, use:

[row, column].Value;

How to get multiple cells:

 range = ()workSheet.get_Range(startCell, endCell); 

Returns a Range object, representing multiple consecutive cells.

4. How to set the cell format in Excel

The way to format the cell is as follows:

(0); 											//Merge method, when 0 is 0, merge it into a cell = 16; 										//Font size = "Bold"; 									//Font = true; 										// Text wrapping automatically(); 									//Automatically adjust the row height = 20; 										//Customize the row height();								//Automatically adjust column width = 15;										//Customize the column width = ; 		//Horizontal center = strText; 										//After merging cells, set the text in it = 20; 							//Fill color = 1; 							//Set the thickness of the cell border

The range can be a single cell or a collection of multiple cells.

XlVAlignis an enumeration class that represents the alignment of values ​​within cells. There are 5 enumeration values ​​inside, and the meanings are as follows

xlVAlignBottom = -4107, //The bottom end is alignedxlVAlignCenter = -4108, //Align centerxlVAlignDistributed = -4117, //Distributed alignmentxlVAlignJustify = -4130, //Align both endsxlVAlignTop = -4160 //Top Alignment

5. How to set the page format in Excel

  = .xlPaperA4;			//Paper size  = ;	//The page horizontal  = 75; 										//Page settings when printing, what percentage of the zoom ratio  = false; 										//The page setting must be set to false, the page height and page width are valid when printing.  = 1; 								//Set the page size to 1 page width  = false; 							//Set page height automatically for page zoom  = "Nigel";								//The logo on the top left of the page  = "Page &P of &N";				//Page Subscript  = true; 							//Print cell mesh line  = 1.5 / 0.035; 							//The upper margin is 2cm (convert to in)  = 1.5 / 0.035; 						//The bottom margin is 1.5cm  = 2 / 0.035; 							//The left margin is 2cm  = 2 / 0.035; 							//The right margin is 2cm  = true; 						//Text level centered

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.