C # Read and write Excel

Source: Internet
Author: User

Online find a lot of methods, there is no prominent focus, anxious to use, a quick summary, the focus is highlighted.
READ:ws.get_range ("A1", Type.Missing);
Write: ws. cells[1, 1] = " modified content ";

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;

Using System.Collections;
Using System.IO;
Using System.Reflection;
Using MSExcel = Microsoft.Office.Interop.Excel;

Namespace Exceltool
{
PublicPartialClass Frmmain:form
{
String str =@"C:\Program files\data\spacekeyword.xlsx";
Object missing = Missing.Value;
Msexcel.application app =Null
Msexcel.workbook WB =Null
Msexcel.worksheet ws =Null
Msexcel.range r =Null
///<summary>
///Load Excel
///</summary>
Privatevoid Initexcel ()
{
//Open Excel
App =New Microsoft.Office.Interop.Excel.Application ();
WB = App. Workbooks.Open (str,False, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing );
App. Visible =False//Read Excel does not show up to affect user experience

//Get Worksheet Object
WS = (msexcel.worksheet) WB. Worksheets.get_item (1);
}
///<summary>
///Read the contents of Excel
///</summary>
Privatevoid Readexcel ()
{
Initexcel ();
//Read A1 cell contents
R = Ws.get_range ("A1", Type.Missing);
string strA1 = R.value;
App. Quit ();//Exit
MessageBox.Show (strA1);
}
///<summary>
///Write to Excel (Win7 to be run as administrator to modify)
///</summary>
Privatevoid Writeexcel ()
{
Initexcel ();
Ws. cells[1,1] ="What's changed";
//Save Excel
Wb. Save ();
Wb. Close (NullNullNULL);
App. Workbooks.close ();
App. Application.Quit ();
App. Quit ();

System.Runtime.InteropServices.Marshal.ReleaseComObject (WS);
System.Runtime.InteropServices.Marshal.ReleaseComObject (WB);
System.Runtime.InteropServices.Marshal.ReleaseComObject (APP);

WS =null;
WB = null;
App = null;
}
}
}

Reference: http://greatverve.cnblogs.com/archive/2010/08/19/csharp-excel.html

Here are some basic things to do with Excel
1: Engineering import of Excel class library, for example: C:\Program Files\Microsoft Office\offiece11\excel.exe
2: Introduction of named Controls: Using Microsoft.office.Interop.Excel;
3: If you are working with an existing Excel file:
Application App=new application ();
Workbook Wbook=app. Workbooks.Open ("C:\\temp.xls", type.missing,type.missing,type.missing,type.missing,type.missing,type.missing, type.missing,type.missing,type.missing,type.missing,type.missing,type.missing,type.missing,type.missing);

Worksheet worksheet= (Worksheet) Wbook. WORKSHEETS[1];

4: If you are creating a new Excel file:
Application App=new application ();
Workbook Wbook=app. Workbook.add (Type.Missing);
Worksheet worksheet= (Worksheet) Wbook. WORKSHEETS[1];

5: Set the contents of a cell:
Worksheet. cells[1,2]= "Column Contents"

6 reading the contents of a cell
String temp= (Range) worksheet. cells[1,2]). Text;

7 Setting the formatting in a cell
Excel.Range rtemp=worksheet.get_range ("A1", "A1");
Rtemp. Font.name= "Song Body";
Rtemp. Font.fontstyle= "Bold";
rt Emp. FONT.SIZE=5;

8 Save new content:
Worksheet. SaveAs ("C:\\temp.xls", type.missing,type.missing,type.missing,type.missing,type.missing,type.missing, type.missing,type.missing);

Url:http://greatverve.cnblogs.com/archive/2012/01/31/csharp-read-write-excel.html

C # Read and write Excel (GO)

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.