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.Collections.Generic;using System.Web;using System.Web.Security;using System.Web.UI;using Sys
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Aspose.Cells;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime;
using System.Text;
 
 
namespace asposecelldemo
{
 
    /// <summary>
    ///
    /// </summary>
    public partial class _Default : System.Web.UI.Page
    {
 
 
        DataTable getData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("name", typeof(string));
            dt.Rows.Add(1, "geovindu");
            dt.Rows.Add(2, "geov");
            dt.Rows.Add(3, "塗斯博");
            dt.Rows.Add(4, "趙雅芝");
            dt.Rows.Add(5, " なわち日本語");
            dt.Rows.Add(6, "처리한다");
            dt.Rows.Add(7, "涂聚文");
            dt.Rows.Add(8, "塗聚文");
            return dt;
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GridView1.DataSource = getData();
                this.GridView1.DataBind();
 
            }
        }
        /// <summary>
        /// http://www.aspose.com/docs/display/cellsnet/Importing+Data+to+Worksheets
        /// </summary>
        /// <param name="table"></param>
        private void ExporttoExcelExcel(DataTable table, string fileName)
        {
            //Instantiate a new Workbook
            Workbook book = new Workbook();
            //Clear all the worksheets
            book.Worksheets.Clear();
            //Add a new Sheet "Data";
            Worksheet worksheet = book.Worksheets.Add("Data");
            HttpContext context = HttpContext.Current;
            context.Response.Clear();
            worksheet.Cells.ImportDataTable(table, true, "A1");
            context.Response.Buffer = true; 
            context.Response.ContentType = "application/ms-excel";
            context.Response.Charset = "utf-8";
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
            context.Response.BinaryWrite(book.SaveToStream().ToArray());
            context.Response.Flush();
            context.Response.End();
                      
        }     
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="dataTable"></param>
        /// <param name="fileName"></param>
        protected void ExportToExcel(DataTable dataTable, string fileName)
        {
             
            HttpContext context = HttpContext.Current;
            StringBuilder sb = new StringBuilder();
 
            //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);
            //} 此法亚洲语言用会出现乱码
            foreach (DataColumn column in dataTable.Columns)
            {
                sb.Append(column.ColumnName + ",");
            }
            sb.Append(Environment.NewLine);
 
            foreach (DataRow row in dataTable.Rows)
            {
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    sb.Append(row[i].ToString() + ",");
                }
                sb.Append(Environment.NewLine);
            }
 
 
            StringWriter sw = new StringWriter(sb);
            sw.Close();
            context.Response.Clear();
            context.Response.Buffer = true;
            context.Response.Charset = "utf-8";
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            context.Response.HeaderEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/csv"; 
            //context.Response.ContentType = "application/ms-excel";
            context.Response.BinaryWrite(new byte[] { 0xEF, 0xBB, 0xBF });
            context.Response.Write(sw);
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8).Replace("+", "%20")+ ".csv");//亂碼
            context.Response.Flush(); 
            context.Response.End();
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            ExportToExcel(getData(), "塗聚文" + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            ExporttoExcelExcel(getData(),"geovindu"+DateTime.Now.ToString("yyyyMMddHHmmssfff"));
        }
 
       
    }
 
}

目录
相关文章
|
11月前
|
关系型数据库 MySQL 数据库
找不到请求的 .Net Framework Data Provider。可能没有安装
做的一个项目,框架为.net framework 数据库为mysql 出现如标题错误 检查是否安装mysql、是否安装mysql connector net 笔者是因为没有安装后者mysql connector net 下载地址: [mysql connector net](https://downloads.mysql.com/archives/c-net/ "mysql connector net") 笔者安装截图如下: ![请在此添加图片描述](https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com/c
154 0
|
4月前
|
数据挖掘 定位技术
.NET Compact Framework下的GPS NMEA data数据分析(二)转
.NET Compact Framework下的GPS NMEA data数据分析(二)转
35 0
|
Web App开发 程序员 数据库
艾伟:为什么微软要推 ADO.NET Data Services Framework
  微软在 .NET 3.5 SP1 平台上,推了一组新的数据访问 Framework,叫做 ADO.NET Data Services。微软怕程序员太闲吗?为什么要创造 ADO.NET Data Services?Web Service 和 WCF 不就很好用了吗?本帖整理一些研讨会及网络上大内高手的观点,并提供一个可在 VS 2008 SP1 上执行的示例给大家下载参考;但本帖不提供 step by step 实作教学,因为网络上已经有一堆这种文章了 (参考本帖最下面的「参考文件」第 6、第 7 点的文章,照着用 VS 2008 + SP1 操作,即可达成本帖提供下载的示例)。
1150 0
|
测试技术
一起谈.NET技术,.Net4.0 Parallel编程(一)Data Parallelism 上
  Parallel.For   首先先写一个普通的循环: private void NormalFor(){for (var i = 0; i private void ParallelFor(){ Parallel.
833 0