Csharp: Importing or Exporting Data from Worksheets using aspose cell, csharpimporting

Source: Internet
Author: User

Csharp: Importing or Exporting Data from Worksheets using aspose cell, csharpimporting

/// <Summary> /// tu juwen /// 20150728 // EXCEL win7 32-bit, 64-bit OK // </summary> public class ExcelHelperImport {/* http://www.cnblogs.com/wangrsh2010/archive/2012/03/21/2410182.html * http://npoi.codeplex.com/SourceControl/latest * http://sourceforge.net/projects/myxls/http://svn.code.sf.net/p/myxls/code/trunk myxls-code * // <summary> /// </ summary> /// <param name = "strFileName"> </param> /// <param name = "inumber"> </param> /// <returns> </ returns> public static System. data. dataTable ReadExcel (String strFileName, int inumber) {Workbook book = new Workbook (); book. open (strFileName); // outdated Worksheet sheet = book. worksheets [inumber]; Cells = sheet. cells; return cells. exportDataTableAsString (0, 0, cells. maxDataRow + 1, cells. maxDataColumn + 1, true );} /// <summary> /// geovindu /// </summary> /// <param name = "strFileName"> </param> /// <param name =" num "> </param> // <returns> </returns> public static DataTable ImportExcel (string strFileName, int num) {Aspose. cells. workbook workbook = new Aspose. cells. workbook (strFileName); // Creating a file stream containing the Excel file to be opened // FileStream fstream = new FileStream (strFileName, FileMode. open); // Instantiating a Workbook object // Opening the Excel file through the file stream // Workbook workbook = new Workbook (fstream ); // Accessing the first worksheet in the Excel file Worksheet worksheet = workbook. worksheets [num]; Cells cells = worksheet. cells; // Exporting the contents of 7 rows and 2 columns starting from 1st cell to able // DataTable = worksheet. cells. exportDataTable (0, 0, 7, 2, true); DataTable dataTable = worksheet. cells. exportDataTable (0, 0, cells. maxDataRow + 1, cells. maxDataColumn + 1, false); // fstream. close (); return dataTable ;} /// <summary> /// geovindu tu juwen // </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "data"> </param> // <param name = "response"> </param> private static void Export <T> (IEnumerable <T> data, httpResponse response, string filename) {Workbook workbook = new Workbook (); Worksheet sheet = (Worksheet) workbook. worksheets [0]; PropertyInfo [] ps = typeof (T ). getProperties (); var colIndex = "A"; foreach (var p in ps) {sheet. cells [colIndex + 1]. putValue (p. name); int I = 2; foreach (var d in data) {sheet. cells [colIndex + I]. putValue (p. getValue (d, null); I ++;} colIndex = (char) (colIndex [0] + 1 )). toString ();} response. clear (); response. buffer = true; response. charset = "UTF-8"; response. appendHeader ("Content-Disposition", "attachment; filename =" + filename + ". xls "); response. contentEncoding = System. text. encoding. UTF8; response. contentType = "application/ms-excel"; response. binaryWrite (workbook. saveToStream (). toArray (); response. end ();} /// <summary> /// Geovin Du /// </summary> /// <param name = "dataTable"> </param> /// <param name = "fileName"> </param> public static void ExportToExcel (DataTable dataTable, string fileName) {HttpContext context = HttpContext. current; context. response. clear (); foreach (DataColumn column in dataTable. columns) {context. response. write (column. columnName + ",");} context. response. write (Environment. newLine); foreach (DataRow row in dataTable. rows) {for (int I = 0; I <dataTable. columns. count; I ++) {context. response. write (row [I]. toString () + ",");} context. response. write (Environment. newLine);} context. response. contentType = "application/MS-excel"; context. response. appendHeader ("Content-Disposition", "attachment; filename =" + fileName + ". csv "); context. response. end ();}}

From: http://www.aspose.com/.net/excel-component.aspx

Https://github.com/heavenwing/WeiXinSDK

Https://github.com/heavenwing/MyWeChatPublic

Https://github.com/geffzhang/opendotnet

Https://github.com/jrsoftware/issrc

 

Http://sourceforge.net/projects/ibatisnet/files/ibatisnet/

Http://sourceforge.net/projects/nhibernate/files? Source = navbar

Http://sourceforge.net/projects/castleproject/files? Source = navbar

Https://github.com/castleproject/

 

 

/// <Summary> /// obtain the worksheet name /// </summary> /// <param name = "strFileName"> </param> /// <returns> </returns> private static DataTable getDataSheetName (string strFileName) {DataTable dt = new DataTable (); dt. columns. add ("id", typeof (int); dt. columns. add ("name", typeof (string); // Aspose. cells. workbook workbook = new Workbook (); // 4.0 // workbook. open (strFileName); /// 4.0 Aspose. cells. workbook workbook = new Aspose. cells. workbook (strFileName); // 7.0 for (int I = 0; I <workbook. worksheets. count; I ++) {dt. rows. add (I, workbook. worksheets [I]. name);} return dt ;} /// <summary> /// obtain the worksheet name /// </summary> /// <param name = "strFileName"> </param> /// <param name = "com"> </param> public static void getSheetName (String strFileName, system. windows. forms. comboBox com) {DataTable dt = getDataSheetName (strFileName); com. dataSource = dt; com. displayMember = "name"; com. valueMember = "id"; com. autoCompleteMode = AutoCompleteMode. suggest; com. autoCompleteSource = AutoCompleteSource. listItems; KillExcelProceed ();} /// <summary> ///// </summary> /// <param name = "strFileName"> file </param> /// <param name =" inumber "> worksheet </param> // <returns> </returns> public static System. data. dataTable ReadExcel (String strFileName, int inumber) {Aspose. cells. workbook book = new Aspose. cells. workbook (strFileName); // 7.0 // Workbook book = new Workbook (); // book. open (strFileName); // 4.0 expired // book. worksheets. count; Worksheet sheet = book. worksheets [inumber]; Cells = sheet. cells; return cells. exportDataTableAsString (0, 0, cells. maxDataRow + 1, cells. maxDataColumn + 1, true );}

  

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.