Open an Excel template file and save the filled data as a file

Source: Internet
Author: User
Recently, you need to open an Excel file template and then fill in the data.
When I started to use oledb to connect to the Excel file, an error always occurred, "an updatable..."
Http://expert.csdn.net/Expert/topic/2992/2992809.xml? Temp =. 4825403.
If access is incorrect, most of the errors are caused by permissions, but what I do is that the winform permission is admin.
Unsuccessful
Later, I used ODBC to test several fields.
Start writing Program Then the two cells are found to be disobedient, prompting a parameter error.
Depressing
Http://expert.csdn.net/Expert/topic/3000/3000113.xml? Temp =. 3331262.
Still unsuccessful
No way to Google
Obtain
Http://www.c-sharpcorner.com/winforms/ExcelReadMG.asp
It is feasible to use COM components. This morning, I asked a netizen PM about how to solve the problem.
Write it here,
The following sections Code To add com references
...........
String strfilename = environment. currentdirectory + @ "\ template. TPL ";
String strsavefilename = environment. currentdirectory + @ "\ Excel \" + system. datetime. now. tostring (). replace (":",""). replace ("-",""). replace ("", "") + @". xls ";
Excel. Application thisapplication = new excel. applicationclass ();
Excel. Workbook thisworkbook;
Object missing = system. reflection. Missing. value;
Try
{
// Load the Excel template file
Thisworkbook = thisapplication. workbooks. Open (strfilename, missing, missing );
Excel. worksheet thissheet = (Excel. worksheet) thisworkbook. Sheets [1];
Thissheet. cells [7,1] = dtend. to1_datestring ();
Progressbar. value = 30;
Thisapplication. Visible = false;
Datarow DR = DT. Rows [0];
For (INT I = 0; I {
Thissheet. cells [7, I + 2] = Dr [I]. tostring ();
Progressbar. Value + = 1;
}
// Save the updated data as a new file
Thissheet. saveas (strsavefilename, missing, missing );
}
Catch {}
Finally
{
Thisapplication. Quit ();
Thisworkbook = NULL;
Thisapplication = NULL;
// DT = NULL;
}
Try
{// Open the generated Excel File
Excel. Workbook newworkbook;
Newworkbook = newapplication. workbooks. Open (strsavefilename, missing, missing );
Excel. worksheet newsheet = (Excel. worksheet) newworkbook. Sheets [1];
Newapplication. Visible = true;

// You can also use system. Diagnostics. process. Start (strsavefilename); to open a new file.

}

An Excel process exists in the process after opening the Excel file.
Manual cleanup required
Refer to this articleArticle

Http://www.eggheadcafe.com/articles/20021012.asp

Http://www.aspxboy.com/476/archive.aspx

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.