Use C # and Excel for report development (4)-copy Cells

Source: Internet
Author: User

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.

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.