Csharp:importing or exporting Data from worksheets using aspose cell

Source: Internet
Author: User

<summary>////20150728////EXCEL///</summary> public class Excelhelperimport { /* http://www.cnblogs.com/wangrsh2010/archive/2012/03/21/2410182.html * http://npoi.codeplex.com/SourceControl/la Test * http://sourceforge.net/projects/myxls/http://svn.code.sf.net/p/myxls/code/trunk Myxls-code *////<summar Y>////</summary>//<param name= "strFileName" ></param>//<par Am Name= "Inumber" ></param>///<returns></returns> public static System.Data.DataTable            Readexcel (String strfilename,int inumber) {Workbook book = new Workbook (); Book. Open (strFileName); Obsolete Worksheet sheet = Book.            Worksheets[inumber]; Cells 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, I            NT 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 (strfile            Name, FileMode.Open); Instantiating a Workbook object////opening the Excel file through the file stream//workbook wo            Rkbook = 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 DataTable//datatable datatable = Wo Rksheet. 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///</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" >&lt ;/param>//<param name= "FileName" ></param> public static void exportToExcel (DataTable datat            Able, 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

Csharp:importing or exporting Data from worksheets using aspose cell

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.