csharp: Importing or Exporting Data from Worksheets using aspose cell

/// <summary>
    /// 涂聚文
    /// 20150728
    /// EXCEL
    /// </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

时间: 2024-11-07 21:16:02

csharp: Importing or Exporting Data from Worksheets using aspose cell的相关文章

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 NVARCHAR(100) NOT NULL, --卡类名称 CardTypeColor NVARCHAR(50) NOT NULL --卡颜色(或样本) ) GO IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Insert_DuCar

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"></param> /// <param name="e"></param> private void btnGenWord_Click(object sender, EventArgs e) { Dictionary<string, string> d

二进制安装MySQL

环境说明: 系统版本Red Hat Enterprise Linux Server release 5.9 MySQL版本mysql-5.7.16 官网下载二进制安装包,因为我的系统为32位,所以我下载的为32位的二进制包:mysql-5.7.16-linux-glibc2.5-i686.tar 开始安装: 建立mysql用户 # groupadd mysql # useradd -g mysql -s /sbin/nologon -r mysql 拷贝二进制包到安装目录 # cp mysql-

mysql 5.6.34 突然宕机,启动不了,提示[ERROR] InnoDB: Space id in fsp header

一.问题描述 一台线上的从服务器,半夜收到报警短信提示异常,连接到该服务器,发现mysqld进程不在了,ps 查看,也没有查到.于是重启,但是重启失败,提示without pid file 于是查看errorlog,内容如下: 2017-09-22 03:37:42 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled 2017-09-

利用.frm、.ibd恢复数据

我们知道启用innodb_file_per_table选项后,单个表(InnoDB引擎)的数据和索引放入单独的文件中(.ibd),建表语句保存在.frm文件中本文假设192.168.85.132,3306实例无法启动(磁盘还能访问),需要将原实例下的replcrash.py_user表恢复到192.168.85.132,3308实例 一..frm得到建表语句 mysqlfrm可以读取.frm文件并从该文件中找到表定义数据生成CREATE语句.在大多数情况下,生成的CREATE语句用于在另一个服务

MySQL压测--注意事项和FAQ

上次我们讲了TPCC的安装和一些使用方法,今天先不着急真正的做MySQL压力测试,先把我最近做压力测试的遇到的一些问题罗列出来,这样 后面做测试可以事半功倍. 1.注意事项 (1).提前规划好具体要测试什么,即测试目的,比如 MySQL5.6与5.7的性能差异: 异步复制和半同步复制的TPS: 设置双1(innodb_flush_log_at_trx_commit=1.sync_binlog=1)性能对数据库性能影响多少: 确认即将上线的新业务对MySQL负载影响多少,是否能承载得住,是否需要对

PXC 避免加入集群时发生SST

环境 现有集群节点: 192.168.99.210:3101 新加入节点: 192.168.99.211:3101 通过xtrabackup备份还原实例,并通过同步方式追数据: 已有节点情况: [email protected] [(none)]>show global status like '%wsrep%'; +----------------------------------+--------------------------------------+ | Variable_name

磁盘空间不够导致mysql崩溃重启

起因: 群里有人提了句pt-ioprofile,我不知道,就查了查,想测一测,想以后可能会有帮助. 为了能看到效果,我选择了我虚拟机上最大的压测表Sbtest1,该表有100w数据,执行update sbtest1 set k=k+1; 并且通过pt-ioprofile查看到了想要的结果,然后就干别的去了,下午了,看update sbtest1 set k=k+1;这个窗口的光标还闪着,以为还没执行完,不停地回车,crtl c,各种不好用.过了一会儿,报错了,并且提示mysql已经重启了. 我去

MySQL提示启动成功,实际进程并没有起来

一.概括: 1.查看运行日志 vim /var/log/mariadb/mariadb.log 2.修改配置文件 vim /etc/my.cnf 3.修改文件权限 chown mysql.mysql -R /var/lib/mysql/ 二.详情: 首先我们尝试启动MySQL,发现没有MySQL进程 [email protected] sbin]# pwd /usr/local/mysql/bin [root@centos74-tmp bin]# service mysqld start Sta