csharp: Importing or Exporting Data from Worksheets using aspose cell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | /// <summary> /// 涂聚文 /// 20150728 /// EXCEL win7 32位,64位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); //过时 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, 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 DataTable //DataTable 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 涂聚文 /// </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/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /// <summary> /// 获取工作表名称 /// </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> /// 获取工作表名称 /// </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">文件</param> /// <param name="inumber">第几个工作表</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 过时 //book.Worksheets.Count; Worksheet sheet = book.Worksheets[inumber]; Cells cells = sheet.Cells; return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true ); } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!