csharp: Importing or Exporting Data from Worksheets using aspose cell
/// <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/
/// <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);
}
csharp: Importing or Exporting Data from Worksheets using aspose cell的更多相关文章
- csharp:asp.net Importing or Exporting Data from Worksheets using aspose cell
using System; using System.Data; using System.Configuration; using System.Collections; using System. ...
- mysql --secure-file-priv is set to NULL.Operations related to importing and exporting data are disabled
--secure-file-priv is set to NULL. Operations related to importing and exporting data are disabledmy ...
- Tutorial: Importing and analyzing data from a Web Page using Power BI Desktop
In this tutorial, you will learn how to import a table of data from a Web page and create a report t ...
- 扩增子分析QIIME2-3数据导出Exporting data
# 激活工作环境 source activate qiime2-2017.8 # 建立工作目录 mkdir -p qiime2-exporting-tutorial cd qiime2-exporti ...
- csharp: ODP.NET,System.Data.OracleClient(.net 4.0) and System.Data.OleDb读取Oracle g 11.2.0的区别
ODP.NET: 引用: using Oracle.DataAccess; //Oracle g 11.2.0 using Oracle.DataAccess.Client; using Oracle ...
- csharp: Procedure with DAO(Data Access Object) and DAL(Data Access Layer)
sql script code: CREATE TABLE DuCardType ( CardTypeId INT IDENTITY(1,1) PRIMARY KEY, CardTypeName NV ...
- abap 通过importing 和 exporting 调用其它函数
1:其它函数的(输入或输出)参数名都在=号左边.
- Csharp: Create Excel Workbook or word from a Template File using aspose.Word 14.5 and aspose.Cell 8.1
winform: /// <summary> /// /// </summary> /// <param name="sender"></ ...
- mysqld: Can't change dir to 'D:\TONG\mysql-5.7.19-winx64\data\' (Errcode: 2 - No such file or directory)
mysqld: Can't change dir to 'D:\TONG\mysql-5.7.19-winx64\data\' (Errcode: 2 - No such file or direct ...
随机推荐
- [GraphQL] Create a GraphQL Schema
we’ll take a look at the GraphQL Language and write out our first GraphQL Schema. We’ll use the grap ...
- 使用 T-SQL 计算当日日期、本周第一天与最后一天
--当日日期 ); SET @Today = DATENAME(YEAR, GETDATE()) + '-' + DATENAME(MONTH, GETDATE()) + '-' + DATENAME ...
- [AX2012]Report data provider调试
运行使用RDP作为数据源的报表时,RDP类被编译成.NET的服务调用,RDP是X++的代码,它的调试是在MorphX调试器中完成.要在MorphX调试器中调试RDP的X++代码需要以下配置: 添加AO ...
- ITF Demo代码(用VBScript构建的接口测试框架)
ITF Demo代码(用VBScript构建的接口测试框架) http://blog.csdn.net/testing_is_believing/article/details/20872629
- windows 获取以及更改CMD控制台编码[转]
本文转自 http://blog.sina.com.cn/s/blog_794b1d96010136yy.html 命令 chcp 功能:显示或设置活动代码页编号 CHCP [nnn] nnn ...
- 基于Java的数据采集(终结篇)
关于写过关于JAVA采集入库的三篇文章: 基于Java数据采集入库(一):http://www.cnblogs.com/lichenwei/p/3904715.html 基于Java数据采集入库(二) ...
- Xenia and Weights(深度优先搜索)
Xenia and Weights time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 蓝凌表单的表体调用Javascript
应用场景:像请假类表单会在从表输入开始时间.结束时间等字段 需求1:客户希望根据开始.结束时间自动计算小时数 解决方法: 1.主表单增加一行,设三个字段[开始时间合计].[结束时间合计].[开始结束时 ...
- C#读写Json
C#处理json文件主要有两种方式: (1)使用JavaScriptSerializer类,需要引入System.Web.Extension库,并添加下面两个引用: using System.Web; ...
- Linux内核中双向链表的经典实现
概要 前面一章"介绍双向链表并给出了C/C++/Java三种实现",本章继续对双向链表进行探讨,介绍的内容是Linux内核中双向链表的经典实现和用法.其中,也会涉及到Linux内核 ...