. NET Operations Excel detailed

Source: Internet
Author: User

Background:

In the project, the need to manipulate the Excel document, this thought is OBA application, but it is not. OBA is embedding the. NET application plug-in in Excel, and we need to manipulate excel in SCSF.

I surveyed a little, mainly found 3 ways.

1: Use Microsoft.Office.Interop.Excel, invoke Excel COM components, manipulate Excel files

2: Use OLE DB to manipulate Excel data sources, and then use Ado.net.

3: Use the open XML to access the Excel ZIP file and use DOM.

Realize:

1: Calling com using. NET

////////////////////////
private static Microsoft.Office.Interop.Excel.Application Xapp
...
if (Xapp = null)
Xapp = new Microsoft.Office.Interop.Excel.ApplicationClass ();
Microsoft.Office.Interop.Excel.Workbook xbook = null;
Xapp.visible = false;
Try
{
Xbook = Xapp.workbooks._open (@ "C:\ pending pay. xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet Xsheet = (Microsoft.Office.Interop.Excel.Worksheet) xbook.sheets[1];
//excel.worksheet xsheet= (excel.worksheet) Xapp.activesheet;
//microsoft.office.interop.excel.range rng1 = Xsheet.get_range ("A1", Type.Missing);
Microsoft.Office.Interop.Excel.Range cell = (Microsoft.Office.Interop.Excel.Range) xsheet.cells[2, 1];
String str = "";
for (int i = 2; cell. Value2!= null; i++)
{
str + = cell. Value2.tostriNg () + ".";
cell = (Microsoft.Office.Interop.Excel.Range) xsheet.cells[2, I];
}
MessageBox.Show (str);
Xbook.close (Microsoft.Office.Interop.Excel.XlSaveAction.xlDoNotSaveChanges, @ "C:\ wages to be paid. xlsx", Missing.Value);
Xapp.quit ();
}
catch (Exception ex)
{
Console.WriteLine (ex. Message.tostring ());
}
///////////////////////

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.