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)