C # Read and Write excel files (html excel files are supported)

Source: Internet
Author: User

This is an excel file exported through Microsoft office. Why is it exported through Microsoft office? Because npoi office is used for export, it does not support one format (excel file in html format ), however, office support is supported. Therefore, write a blog, share it with you, and share it with you.

Method of thinking: Read the excel file of the file, and then export the corresponding format through the template format

////// Export excel //////Read File Path///Save File ContentPublic static void ReadFile (string readFileName, string saveFileName) {Microsoft. office. interop. excel. workbookClass wbclass; WorkbookClass wkTemp; Worksheet ws; Worksheet wsTemp; Microsoft. office. interop. excel. application excelApp; DirectoryInfo di = new DirectoryInfo (readFileName); // traverses the folder if (di. exists) {foreach (FileInfo item in di. getFiles ("*. xls ") {excelApp = new Microsoft. office. interop. E Xcel. application (); object objOpt = System. reflection. missing. value; // item. fullName reads the execl file wbclass = (WorkbookClass) excelApp. workbooks. open (item. fullName, objOpt, false, objOpt, true, objOpt, objOpt, true, objOpt, objOpt); ws = (Worksheet) wbclass. worksheets. get_Item (1); // get the total number of record rows (including the title column) int rowsint = ws. usedRange. cells. rows. count; // get the number of rows // int columnsin T = mySheet. usedRange. cells. columns. count; // obtain the number of columns Range rng1 = ws. cells. get_Range ("C1", "C" + rowsint); // rang Range rng2 = ws. cells. get_Range ("D1", "D" + rowsint); // rang Range rng3 = ws. cells. get_Range ("E1", "E" + rowsint); // rang Range rng4 = ws. cells. get_Range ("F1", "F" + rowsint); // rang object [,] CallType = (object [,]) rng1.Value2; object [,] activeCall = (object [,]) rng2.Value2; object [,] Passi VityCall = (object [,]) rng3.Value2; object [,] CallDate = (object [,]) rng4.Value; // read wkTemp = (WorkbookClass) excelApp through a template. workbooks. open ("D :\\ Template.xls", objOpt, false, objOpt, true, objOpt, objOpt, true, objOpt, objOpt ); wsTemp = (Worksheet) wkTemp. worksheets. get_Item (1); Range rngD = (Range) wsTemp. columns ["D", Type. missing]; // set the cell format Range rngE = (Range) w STemp. columns ["E", Type. missing]; // set the cell format rngD. numberFormat = "yyyy-mm-dd hh: mm: ss"; rngE. numberFormat = "hh: mm: ss"; // write the information excel file for (int I = 1; I <rowsint; I ++) {wsTemp. cells [I, 1] = CallType [I, 1]. toString (); wsTemp. cells [I, 2] = ActiveCall [I, 1]. toString (); wsTemp. cells [I, 3] = PassivityCall [I, 1]. toString (); wsTemp. cells [I, 4] = PassivityCall [I, 1]. toString ();} wsTemp. saveAs (saveFile Name + "\ 1.xls", Missing. value, Missing. value, Missing. value, Missing. value, Missing. value, Missing. value, Missing. value, Missing. value, Missing. value); excelApp. workbooks. close (); // Close the opened document. Otherwise, the student ID will display the scientific notation. ExcelApp. Quit ();}}}

  

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.