Use VSTO to copy some Excel cells (formatted) and paste and save to another Excel

Source: Internet
Author: User

Recently used VSTO to do a small project, where one of the requirements is to save a number of formatted areas of an Excel worksheet in a separate Excel file, requiring that the source format be preserved.

Although the requirements are simple, there are a few technical points to figure out:

1. With format copy, but only copy values and formats, do not copy formulas, references, checksums and so on.

2. Save As Excel

The first question, I thought it was very simple, direct use paste, paste all on the line, in fact, is not so simple. Start using the following code:

PasteSpecial

ws.get_Range(beginCell, pasteRange).PasteSpecial

(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteAll,

Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,

Type.Missing, Type.Missing);

But all the values were copied and the format was not copied. So the study of Microsoft.Office.Interop.Excel.XlPasteType, found that there are more than 10 options, one by one, found that the use of any single enumeration does not meet my requirements. Later brainwave, the use of two replication to solve the problem, one copy of the value, one copy format! The code is as follows:

PasteSpecial2

ws.get_Range(beginCell, pasteRange).PasteSpecial

(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues,

Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,

Type.Missing, Type.Missing);
ws.get_Range(beginCell, pasteRange).PasteSpecial

(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteFormats,

Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,

Type.Missing, Type.Missing);

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.