Report Development using C # and Excel (4)

Source: Internet
Author: User

When you use Excel as a report, you may encounter paging, which is divided into multiple sheet display, if you want to keep the header on each page, you need to copy the first page as a template design of the header part of the other sheet, then use the copy of the cell in Excel.

The following code shows how to copy the selection on one sheet to another sheet:

 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 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, TYP E.missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

As you can see, the core code is very simple, is to select one of the source areas and then specify another target area, and the copy method of the source area is called to copy the entire content to the target area, but you will find that the properties of the cell are copied together, if you want to copy only the text in the cell? It's going to use the Windows clipboard:

xlSheet = (Excel.Worksheet)ThisWorkbook.Worksheets.get_Item(1);
Excel.Range 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 this is a way to select only one cell, copy one, and not select a batch of cells to copy.

The above example only gives the main part of the code, other such as the destruction of Excel processes such as operations, please refer to the previous several Excel for the report of the essay.

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.