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;
}
}
}