When using Excel as a report, the page may be displayed, which is divided into multiple sheets. If you want to retain the header on each page, you need to copy the first page as the header of the template design to another sheet, then you need to use cells in Excel to copy.
The following code copies the selected content on one sheet to another:
Thisapplication = new excel. Application ();
Thisworkbook = thisapplication. workbooks. open ("Z: \ book1.xls", type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing );
Thisapplication. displayalerts = false;
Xlsheet = (Excel. worksheet) thisworkbook. worksheets. get_item (1 );
Excel. Range = xlsheet. get_range ("A1", type. Missing );
Range. value = "123 ";
Excel. worksheet sheet1 = (Excel. worksheet) thisworkbook. worksheets. get_item (2 );
Excel. Range range1 = sheet1.get _ range ("B1", type. Missing );
Range. Copy (range1 );
Thisworkbook. saveas ("Z: \ book2.xls", type. Missing, type. Missing,
Type. Missing, Excel. xlsaveasaccessmode. xlnochange,
Type. Missing, type. Missing );
As you can see, the core code is very simple, that is, to select a source region, specify another target region, and call the copy method of the source region to copy the entire content to the target region, however, you will find that the format and other attributes of the cell are copied together. What if you want to copy only the text in the cell? The Windows clipboard is required:
Xlsheet = (Excel. worksheet) thisworkbook. worksheets. get_item (1 );
Excel. Range = xlsheet. get_range ("A1", type. Missing );
Range. value = "123 ";
System. Windows. Forms. clipboard. setdataobject (range. value. tostring ());
Excel. worksheet sheet1 = (Excel. worksheet) thisworkbook. worksheets. get_item (2 );
Excel. Range range1 = sheet1.get _ range ("B1", type. Missing );
Sheet1.paste (range1, false );
Note that only one cell can be selected and one cell can be copied. You cannot select a batch of cells for replication.
The above example only shows the main part of the Code. For other operations such as destroying an Excel process, refer to the previous articles on Excel as a report.