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

  

        /// <summary>
/// http://www.aspose.com/docs/display/cellsnet/Importing+Data+to+Worksheets
/// </summary>
/// <param name="table"></param>
private void ExporttoExcelExcel(DataTable table, string fileName,int type)
{
//Instantiate a new Workbook
Workbook book = new Workbook();
// book.Save("",SaveFormat.Xlsx);
//Clear all the worksheets
book.Worksheets.Clear();
//Add a new Sheet "geovindu";
Worksheet worksheet = book.Worksheets.Add("geovindu");
HttpContext context = HttpContext.Current;
context.Response.Clear();
worksheet.Cells.ImportDataTable(table, true, "A1"); context.Response.Buffer = true;
if (type == 1)
{
context.Response.ContentType = "application/ms-excel"; //2003
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();
} if (type == 2)
{
//1.//不可以 ,擴展名出現問題
//context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // 2007
//context.Response.Charset = "utf-8";
//context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
////book.Save("", SaveFormat.Xlsx);
//context.Response.BinaryWrite(book.SaveToStream().ToArray()); ////Save with default format, send the file to user so that he may open the file in
////some application or save it to some location
//// book.Save(this.Response, "importeddata.xlsx", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Xlsx));
//context.Response.Flush();
//context.Response.End(); //2.
string sb = DataTabletoHmtl(table);
// string sb = toHTML_Table(table);
byte[] array = Encoding.UTF8.GetBytes(sb.ToString());
MemoryStream ms = new MemoryStream(array);
LoadOptions lo = new LoadOptions(LoadFormat.Html);
book = new Workbook(ms, lo);
worksheet = book.Worksheets.Add("geovindu");
book.Save(Response, fileName + ".xlsx", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Xlsx));
} }
/// <summary>
///
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public string DataTabletoHmtl(DataTable dt)
{
if (dt.Rows.Count == 0)
return ""; string tab = "\t"; StringBuilder sb = new StringBuilder(); sb.AppendLine("<html>");
sb.AppendLine(tab + "<body>");//不帶HTML頭,會顯示格式問題.塗聚文註
sb.AppendLine(tab + tab + "<table>"); // headers.
sb.Append(tab + tab + tab + "<tr>"); foreach (DataColumn dc in dt.Columns)
{
sb.AppendFormat("<td>{0}</td>", dc.ColumnName);
} sb.AppendLine("</tr>"); // data rows
foreach (DataRow dr in dt.Rows)
{
sb.Append(tab + tab + tab + "<tr>"); foreach (DataColumn dc in dt.Columns)
{
string cellValue = dr[dc] != null ? dr[dc].ToString() : "";
sb.AppendFormat("<td>{0}</td>", cellValue);
} sb.AppendLine("</tr>");
} sb.AppendLine(tab + tab + "</table>");
sb.AppendLine(tab + "</body>");
sb.AppendLine("</html>");
return sb.ToString();
} /// <summary>
///
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public string toHTML_Table(DataTable dt)
{
if (dt.Rows.Count == 0)
return ""; StringBuilder builder = new StringBuilder();
builder.Append("<html>");
builder.Append("<head>");
builder.Append("<title>");
builder.Append("Page-");
builder.Append(Guid.NewGuid().ToString());
builder.Append("</title>");
builder.Append("</head>");
builder.Append("<body>");
builder.Append("<table border='1px' cellpadding='5' cellspacing='0' ");//有樣式,會提示有格式問題. 塗聚文註
builder.Append("style='border: solid 1px Silver; font-size: x-small;'>");
builder.Append("<tr align='left' valign='top'>");
foreach (DataColumn c in dt.Columns)
{
builder.Append("<td align='left' valign='top'><b>");
builder.Append(c.ColumnName);
builder.Append("</b></td>");
}
builder.Append("</tr>");
foreach (DataRow r in dt.Rows)
{
builder.Append("<tr align='left' valign='top'>");
foreach (DataColumn c in dt.Columns)
{
builder.Append("<td align='left' valign='top'>");
builder.Append(r[c.ColumnName]);
builder.Append("</td>");
}
builder.Append("</tr>");
}
builder.Append("</table>");
builder.Append("</body>");
builder.Append("</html>"); return builder.ToString();
}

 net4.0

        public string ConvertDataTableToHTMLTableInOneLine(DataTable dt)
{
//Convert DataTable To HTML Table in one line
return "<table>\n<tr>" + string.Join("", dt.Columns.Cast<DataColumn>().Select(dc => "<td>" + dc.ColumnName + "</td>")) + "</tr>\n" +
"<tr>" + string.Join("</tr>\n<tr>", dt.AsEnumerable().Select(row => "<td>" + string.Join("</td><td>", row.ItemArray) + "</td>").ToArray()) + "</tr>\n<\table>"; }

  

csharp:asp.net Importing or Exporting Data from Worksheets using aspose cell的更多相关文章

  1. csharp: Importing or Exporting Data from Worksheets using aspose cell

    /// <summary> /// 涂聚文 /// 20150728 /// EXCEL win7 32位,64位OK /// </summary> public class ...

  2. 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 ...

  3. ASP.NET Core 数据保护(Data Protection 集群场景)【下】

    前言 接[中篇],在有一些场景下,我们需要对 ASP.NET Core 的加密方法进行扩展,来适应我们的需求,这个时候就需要使用到了一些 Core 提供的高级的功能. 本文还列举了在集群场景下,有时候 ...

  4. ASP.NET Core 数据保护(Data Protection)【中】

    前言 上篇主要是对 ASP.NET Core 的 Data Protection 做了一个简单的介绍,本篇主要是介绍一下API及使用方法. API 接口 ASP.NET Core Data Prote ...

  5. ASP.NET Core 数据保护(Data Protection)【上】

    前言 上一篇博客记录了如何在 Kestrel 中使用 HTTPS(SSL), 也是我们目前项目中实际使用到的. 数据安全往往是开发人员很容易忽略的一个部分,包括我自己.近两年业内也出现了很多因为安全问 ...

  6. 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 ...

  7. Asp.net core 学习笔记 ( Data protection )

    参考 : http://www.cnblogs.com/xishuai/p/aspnet-5-identity-part-one.html http://cnblogs.com/xishuai/p/a ...

  8. 扩增子分析QIIME2-3数据导出Exporting data

    # 激活工作环境 source activate qiime2-2017.8 # 建立工作目录 mkdir -p qiime2-exporting-tutorial cd qiime2-exporti ...

  9. csharp:ASP.NET SignalR

    http://signalr.net/ https://github.com/SignalR/SignalR http://www.asp.net/signalr http://www.cnblogs ...

随机推荐

  1. Aspose转PDF时乱码问题的解决

    主要原因是服务器上一般安装的字体都是有限的,而我们日常生活工作中总是喜欢用一些比较特别的字体,比如宋体GB2312,这时候如果用Aspose转PDF就会出现乱码,解决方法也比较简单,把本地的特殊字体拷 ...

  2. 为MFC界面添加一个Log Window

    前言 由于早期的图像处理程序基于VC6.0,MFC也是采用VC6.0开发的.在实际处理中,我不仅需要界面的显示,有很多时候,我需要算法处理的过程中的信息,比如每个阶段的耗时,处理的图像大小,以及如果需 ...

  3. 微软MVC对架构的一点思考

    毕业即将三年,在学校学习.做毕设一直使用拖控件的 Winform\WebForm,工作后公司采用MVC3架构做项目. 下面使用mvc的个人总结 : 1.架构上分层清晰.便于研发,耦合性好 2.缓存机制 ...

  4. 利用VBA查找excel中一行某列第一次不为空与最后一列不为空的列数

    昨日同事有需求,想知道每个商品第一次销售的月份,以及最后一次销售的月份. 本想通过什么excel函数来解决,但是找了半天也没找到合适的,最后还是通过VBA来解决吧. 使用方法: Excel工具-宏-V ...

  5. 并行编程多线程之Parallel

    1.简介 随着多核时代的到来,并行开发越来越展示出它的强大威力!使用并行程序,充分的利用系统资源,提高程序的性能.在.net 4.0中,微软给我们提供了一个新的命名空间:System.Threadin ...

  6. ubuntu 搭建 samba 服务器

    . sudo apt-get install samba samba-common . sudo vi /etc/samba/smb.conf [alair's share] path = /home ...

  7. Money类

    public class Money { /// <summary> /// 要转换的数字 /// </summary> private double j; private s ...

  8. 疯狂的ASP.NET系列-第一篇:啥是ASP.NET后续

    之前总结到了ASP.NET的七大特点,只总结了2大特点,现继续总结后面的5大特点. (3)ASP.NET支持多语言 这里说的多语言就是多种开发语言,如C#,VB.NET,无论你采用哪种开发语言,最终的 ...

  9. 分享27款最佳的复古风格 WordPress 主题

    WordPress 作为最流行的博客系统,插件众多,易于扩充功能.安装和使用都非常方便,而且有许多第三方开发的免费模板,安装方式简单易用. 复古风格可以应用于任何东西,从服装到室内设计,那么复古风格的 ...

  10. Configuring Service Broker for Asynchronous Processing

    Configuring Service Broker for Asynchronous Processing --create a database and enable the database f ...