C # Create, save Excel normal execution essentials

Source: Internet
Author: User

Online Search for C # Implementation of Excel operations is too many examples, but do not know how many of the verification is actually feasible to release, but also because of development needs, I found some code but find that most of them can not be completed correctly, so decided to supplement their own in practice encountered in the main points for reference. The following example:

using Microsoft.Office.Interop.Excel;
using System.Reflection;

public class MyConsole
{
public static void Main ()
{
Application app = new ApplicationClass () ;
Try
{
//Let background execution set to invisible
app. Visible = false;
//Add a new workbook
Workbook Wbook = App. Workbooks.Add (TRUE);
//If you want to open an existing workbook, use the following comment statement
//Workbook Wbook = App. Workbooks.Open (@ "C:\YourPath\YourWorkbook.xls",
//Missing, missing, missing, missing, missing, missing, missin G,
//Missing, missing, missing, missing, missing,missing, missing);

//Get a worksheet
//If an existing workbook is open, you can also get the worksheet worksheet Wsheet = wbook.activesheet as worksheet
Wo Rksheet Wsheet = wbook.worksheets[1] as worksheet;       

Wsheet.cells[1, 1] = "This is a test";
Set the query prompt box to prevent pop-up save and overwrite
app. DisplayAlerts = false;
App. Alertbeforeoverwriting = false;
//Save Workbook
Wbook.Save ();
//Save Excel File
app. Save ("C:\\abc.xls");
}
Catch
{
}
finally
{
//Ensure that the Excel process closes the
app. Quit ();
app = null;
}
}
}

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.