. NET Operations Excel instance sharing

Source: Internet
Author: User

  This article mainly introduces. NET to manipulate Excel instances, you need friends to refer to the

1. Read   read as if there are several ways, through ado.net, through the Microsoft.Interop.Excel support class library with invoke COM read, as well as through the zip decompression of the final read Dom (this seems quite complicated) Here I use the ado.net only to introduce this one.     Code as follows: Public DataTable exceltodatatable (string strexcelpath, String strsheetname) {  string strconn =&N Bsp     "provider=microsoft.jet.oledb.4.0" + "Data source=" + Strexcelpath + ";" + "Extended Properties=excel 5.0;" ;   String strexcel = string. Format ("select * from [{0}$]", strsheetname);   DataSet ds = new DataSet ();   OleDbDataAdapter adapter = new OleDbDataAdapter (Strexcel, strconn);   Adapter. Fill (ds, strSheetName);   Conn. Close ();   Return DS. Tables[strsheetname];     Parameters: Strexcelpath Excel file path, strSheetName to read the name of the table here to read SheetName code, this is called Microsoft.Interop.Excel to read     Excel.Workbook theworkbook = ExcelObj.Workbooks.Open ("Excel file path", 0, True, 5, "", "", True, Excel.xlplatform . Xlwindows, "T", false, False, 0, true);     Excel.Sheets Sheets = theworkbook.worksheets;     Excel.Worksheet worksheet = (Excel.Worksheet) sheets.get_item (1);     execname = worksheet. Name;     theworkbook.close (null, NULL, NULL);/Remember to close, otherwise the program closes, and the Excel process is still 2. Create a new Excel file   code as follows: Excel.applicationclass myexcel = new Excel.applicationclass ();      //instance an Excel excel._workbook xbk;      //work thin   equivalent to an Excel file                  Excel._workshe ET xSt;      //Work sheet     The table in a file XBK = MyExcel.Workbooks.Add (true);      //allows adding sheet object missing = system.reflection.missing.value;//null value MYEXCEL.SHEETS.ADD (missing, Missing, 1, Excel.XlSheetType.xlWorksheet)//Add sheet xSt = (excel._worksheet) xbk.activesheet;//get default sheet Xst.name = " New Table ";      //Set table name Xbk.saveas ("Save Path", missing, missing,     missing, missing, missing, Excel.xlsavea saccessmode.xlshared,     missing, missing, missing, missing);     &NBsp;//Save File Myexcel.quit (); Also remember to close     3. Add content here just to say that the general data added, the chart is not introduced, in the operation of Excel is generally in the table and range as a unit, the row or column is starting from 1 rather than starting from 0 first introduced the format, as follows: Set the first line of the font is red. ((Excel.Range) xst.rows[1, Type.Missing]). Font.Color = 0xff0000;      //XST is the variable name for the top code, and the color value is the format of the second column as a percentage ((Excel.Range) xst.columns[2, Type.Missing) with 16-in-RGB settings. NumberFormat = "0%";       Common format: Text: @ Date: yyyy/mm/dd   Number: #,# #0   Currency: ¥#,# #0.  %: 0% The top two are row selection and column selection, Use Get_range () If you want to select Line 1th, column 2nd, and 5th column 1th.       Xst.get_range (xst.cells[1,2],xst.cells[1,5]) If you need other formats, such as cell background, border, font style, check the file below. However, most of the method from the point of the name can know and then add content, directly write point code reference.     Code as follows: int rowidx = 2; Starting from the 2nd row//Here dt is a DataTable data source foreach (DataRow dr in Dt. Rows) { int j = 1;      //starting from 1th column  myexcel.cells[rowidx, j + +] = dr["dt column name"]. ToString ();    myexcel.cells[rowidx, j + +] = dr["dt column name"]. ToString ();    myexcel.cells[rowidx, j + +] = dr["dt column name"]. ToString ();    myexcel.cells[rowidx, j + +] = dr["dt Column Name"]. ToString ();    myexcel.cells[rowidx, j + +] = dr["dt column name"]. ToString ();    myexcel.cells[rowidx, j + +] = dr["dt column name"]. ToString ();   //shows the a+b+c+d result  myexcel.cells[rowidx, j + +] = string in the case of a formula. Format ("=sum (a{0}:D {0})", Rowidx);   rowIdx++;}     finished writing Save Xbk.save (), or remember to close.    

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.