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"));
        }
 
       
    }
 
}

目录
相关文章
|
7月前
|
开发框架 .NET API
ASP.NET Core Binding source
Binding source Attributes借助 binding source attributes 可以将请求传递的数据传递给 api,详细的attribute 如下图。但 在 api 项目下的 标识了 apicontroller 的controller下,binding source attributes 的规则 有所改变 HttpHEADhead 也是一种http 请求方法,他...
24 0
ASP.NET Core Binding source
|
.NET 数据库 开发框架
ASP.NET CORE的Code Fist后Models更改了怎么办?
上次我写到MVC的code fist后,自动生成数据库并自动生成web页面了 点击打开链接   那么随着项目需求的逐步明确,model变化了怎么办呢?其实和上次一样的,有两条关键的语句要记住 Add-Migration Initial Update-Database 其中Initial是可以任意的,且不能重复   依次在程序包管理控制台中输入以上两条语句即可实现将model的变化同步到数据库中。
907 0
|
前端开发 .NET 开发框架
|
前端开发 JavaScript 数据安全/隐私保护