C # basic Excel Import and Export Methods

Source: Internet
Author: User

We often submit it to an Excel file. Let's talk about how to operate Excel in the C # program today. Here, we mainly import and export data. On the page, you may often see clicking a button to download the display information of controls such as GridView. How does one download data in a program? In fact, the simple method is to save this set of regular data to the local file system in Excel mode. Let me call this method a "program method. The following shows the specific Code implementation: View Code when we call this method by clicking the Button, we will be surprised to find that the page is running, as shown in the following scenarios: are you "stunned. Haha, it is clear that my control Gridview is placed in a form with runat = server? In fact, you only need to override the VerifyRenderingInServerForm event. The Code is as follows: View Code here describes a similar solution. You do not need to rewrite the above event. The method is as follows: View Code is a lot of similar methods) both are written to the local file system by writing the HTTP Response output stream. We know that Excel is a small database model. I would like to call it the "Oledb" method. Therefore, we can consider using data access to achieve the above effect. First, introduce Microsoft. Office. Interop. Excel; View Code and above are the export of Excl. Let's take a look at the View Code imported from Excel. connection strings are involved here. For details, see: string strConn = @ "Provider = Microsoft. jet. oleDb.4.0; "+" data source = "+ filePath +"; Extended Properties = 'excel 8.0; HDR = Yes; IMEX = 1' "; // This connection can only operate on the top of excel2007(.xls) file string strCon = @ "Provider = Microsoft. ace. oleDb.12.0; "+" data source = "+ fileName +"; Extended Properties = 'excel 12.0; HDR = Yes; IMEX = 1' "for different office versions, the connection string is also different. The idea is that when we use a program to export data and then import data using Excel "Oledb", the following problems may occur. Check: This is a non-standard excel file caused by program export. This is a disadvantage of program export. Appropriate processing is required. During the export process, we will introduce it here for time reasons. The following is my ExcelOperation class: View Code Haha. It's a bit late. I'm going to bed for cainiao. Prepare for tomorrow's miserable life. January 16.

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.