基于 Aspose.Cells for .NET 8.5.0 工具类,

Aspose.Cells for .NET 8.5.0 这个自己去CSDN下载里面有破解的,没有破解的导出excel的时候会(Aspose.Cells)这样的logo

var workbook = new Workbook(fstream, loadOptions); 
创建Workbook对象建议传入需要格式:new LoadOptions(LoadFormat.Xlsx)(可以指定自己需要的格式),
不然后面使用workbook解析Excel和导出Excel的时候会报格式化出错,官网里面建议传入需要格式的LoadOptions。 老版本的Aspose.Cells 使用的是save和load的时候传入loadOptions,这些方法在高版本中已经过期了,
高版本是在创建对象的时候传入的。
response.ContentType = "application/ms-excel"; 自己设置需要导出的格式,
另外附上ContentType 对照表:http://tool.oschina.net/commons  
/// <summary>
/// AsposeExcel 帮助类
/// </summary>
public class AsposeExcelUtil
{
private const int BufferSize = 0x1000;
/// <summary>
/// 读取Excel文件
/// </summary>
/// <param name="fullFilename">文件地址</param>
/// <returns></returns>
public static System.Data.DataTable ReadExcel(String fullFilename)
{
return ReadExcel(fullFilename, new LoadOptions(LoadFormat.Xlsx));
} /// <summary>
/// 读取Excel文件
/// </summary>
/// <param name="fullFilename">文件地址</param>
/// <param name="loadOptions">参数信息【如:读取包含密码的excel】</param>
/// <returns></returns>
public static System.Data.DataTable ReadExcel(String fullFilename, LoadOptions loadOptions)
{
var fstream = new System.IO.FileStream(fullFilename, FileMode.Open);
var workbook = new Workbook(fstream, loadOptions); Worksheet sheet = workbook.Worksheets[]; Cells cells = sheet.Cells;
return cells.ExportDataTableAsString(, , cells.MaxDataRow + , cells.MaxDataColumn + , true);
} private static void MakeHeader(Worksheet sheet, DataTable dt)
{
for (int col = ; col < dt.Columns.Count; col++)
{
Cell cell = sheet.Cells[, col];
cell.SetStyle(MakeHealStyle());
}
} /// <summary>
/// 创建默认的excel头样式
/// </summary>
/// <returns></returns>
private static Style MakeHealStyle()
{
var style = new Style();
style.Font.Name = "微软雅黑";//文字字体
style.Font.Size = ;//文字大小
style.IsLocked = false;//单元格解锁
style.Font.IsBold = true;//粗体
style.Font.IsBold = true;
style.Pattern = BackgroundType.Solid; //设置背景样式
style.IsTextWrapped = true;//单元格内容自动换行
style.SetBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);
style.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style.IsTextWrapped = false;//setTextWrapped
return style;
} private static void MakeBody(Worksheet sheet, DataTable dt)
{
for (int r = ; r < dt.Rows.Count; r++)
{
for (int c = ; c < dt.Columns.Count; c++)
{
sheet.Cells[r + , c].PutValue(dt.Rows[r][c].ToString());
}
}
} /// <summary>
/// 把dt转换成excel的字节数组【不建议使用】
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static Byte[] DatatableToExcelBytes(DataTable dt)
{
try
{
return DatatableToExcelStream(dt).ToArray();
}
catch (Exception e)
{
return null;
}
} /// <summary>
/// 把dt转换成excel的流
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static MemoryStream DatatableToExcelStream(DataTable dt)
{
try
{
var workbook = new Workbook();
var sheet = workbook.Worksheets[];
sheet.Cells.ImportDataTable(dt, true, , );
MakeHeader(sheet, dt);
sheet.AutoFitColumns();
sheet.AutoFitRows();
var memory = new MemoryStream();
workbook.Save(memory, SaveFormat.Xlsx);
memory.Seek(, SeekOrigin.Begin);
return memory;
}
catch (Exception e)
{
return null;
}
} /// <summary>
/// 根据Dt生成 excel文件
/// </summary>
/// <param name="dt"></param>
/// <param name="exportFileName"></param>
/// <returns></returns>
public static Boolean DatatableToExcel(DataTable dt, string exportFileName)
{
try
{
var workbook = new Workbook();
var sheet = workbook.Worksheets[];
sheet.Cells.ImportDataTable(dt, true, , );
MakeHeader(sheet, dt);
sheet.AutoFitColumns();
sheet.AutoFitRows();
workbook.Save(exportFileName, SaveFormat.Xlsx);
return true;
}
catch (Exception e)
{
return false;
}
} /// <summary>
/// 将集合对象装换成Excel的字节数组
/// </summary>
/// <typeparam name="T">对象的类型</typeparam>
/// <param name="data">集合</param>
public static byte[] ConvertToExportByte<T>(IEnumerable<T> data)
{
return ConvertToExportStream<T>(data).ToArray();
} /// <summary>
/// 将对象转换成流
/// </summary>
/// <param name="data"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static MemoryStream ConvertToExportStream<T>(IEnumerable<T> data)
{
var workbook = new Workbook();
var sheet = workbook.Worksheets[];
PropertyInfo[] infos = typeof(T).GetProperties();
var colIndex = "A";
foreach (var p in infos)
{
Cell cell = sheet.Cells[colIndex + ];
Style style = MakeHealStyle();
cell.SetStyle(style);
cell.PutValue(p.Name);
int i = ;
foreach (var d in data)
{
sheet.Cells[colIndex + i].PutValue(p.GetValue(d, null));
i++;
}
colIndex = ((char)(colIndex[] + )).ToString(CultureInfo.InvariantCulture);
}
var memory = new MemoryStream();
workbook.Save(memory, SaveFormat.Xlsx);
memory.Seek(, SeekOrigin.Begin);
return memory;
} /// <summary>
/// 根据 datatable 导出文件
/// </summary>
/// <param name="data"></param>
/// <param name="response"></param>
/// <param name="exportFileName"></param>
public static void Export(DataTable data, HttpResponse response, String exportFileName)
{
response.Clear();
response.Buffer = true;
response.Charset = "UTF8";
response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(exportFileName, Encoding.UTF8).ToString(CultureInfo.InvariantCulture));
response.ContentEncoding = System.Text.Encoding.UTF8;
response.ContentType = "application/ms-excel";
Stream outputStream = DatatableToExcelStream(data);
var buffer = new byte[BufferSize];
int bytesRead = ;
while ((bytesRead = outputStream.Read(buffer, , BufferSize)) > )
{
response.OutputStream.Write(buffer, , bytesRead);
}
outputStream.Close();
response.Flush();
response.End();
} /// <summary>
/// 根据 datatable 导出文件
/// </summary>
/// <param name="data"></param>
/// <param name="response"></param>
/// <param name="exportFileName"></param>
public static void Export(DataTable data, HttpResponseBase response, String exportFileName)
{
response.Clear();
response.Buffer = true;
response.Charset = "UTF8";
response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(exportFileName, Encoding.UTF8).ToString(CultureInfo.InvariantCulture));
response.ContentEncoding = System.Text.Encoding.UTF8;
response.ContentType = "application/ms-excel";
Stream outputStream = DatatableToExcelStream(data);
var buffer = new byte[BufferSize];
int bytesRead = ;
while ((bytesRead = outputStream.Read(buffer, , BufferSize)) > )
{
response.OutputStream.Write(buffer, , bytesRead);
}
outputStream.Close();
response.Flush();
response.End();
} /// <summary>
/// 导出数据
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="data">集合</param>
/// <param name="response">HttpResponse</param>
/// <param name="exportFileName">导出的文件名称</param>
public static void Export<T>(IEnumerable<T> data, HttpResponse response, String exportFileName)
{
response.Clear();
response.Buffer = true;
response.Charset = "UTF8";
response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(exportFileName, Encoding.UTF8).ToString(CultureInfo.InvariantCulture));
response.ContentEncoding = System.Text.Encoding.UTF8;
response.ContentType = "application/ms-excel";
Stream outputStream = ConvertToExportStream<T>(data);
var buffer = new byte[BufferSize];
int bytesRead = ;
while ((bytesRead = outputStream.Read(buffer, , BufferSize)) > )
{
response.OutputStream.Write(buffer, , bytesRead);
}
outputStream.Close();
response.Flush();
response.End();
} /// <summary>
/// 导出数据
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="data">集合</param>
/// <param name="response">HttpResponse</param>
/// <param name="exportFileName">导出的文件名称</param>
public static void Export<T>(IEnumerable<T> data, HttpResponseBase response, String exportFileName)
{
response.Clear();
response.Buffer = true;
response.Charset = "UTF8";
response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(exportFileName, Encoding.UTF8).ToString(CultureInfo.InvariantCulture));
response.ContentEncoding = System.Text.Encoding.UTF8;
response.ContentType = "application/ms-excel";
Stream outputStream = ConvertToExportStream<T>(data);
var buffer = new byte[BufferSize];
int bytesRead = ;
while ((bytesRead = outputStream.Read(buffer, , BufferSize)) > )
{
response.OutputStream.Write(buffer, , bytesRead);
}
outputStream.Close();
response.Flush();
response.End();
}
}

Aspose.Cells for .NET 8.5.0 工具类的更多相关文章

  1. 基于 Aspose.Cells与XML导入excel 数据----操作类封装

    前言 导入excel数据, 在每个项目中基本上都会遇到,第三方插件或者基于微软office,用的最多的就是npoi,aspose.cells和c#基于office这三种方式,其中各有各的优缺点,在这也 ...

  2. 使用Aspose.Cells 根据模板生成excel里面的 line chart

    目的: 1.根据模板里面的excel数据信息,动态创建line chart 2.linechart 的样式改为灰色 3.以流的形式写到客户端,不管客户端是否装excel,都可以导出到到客户端 4.使用 ...

  3. 使用Aspose.Cells生成Excel的线型图表

    目的: 1.根据模板里面的excel数据信息,动态创建line chart 2.linechart 的样式改为灰色 3.以流的形式写到客户端,不管客户端是否装excel,都可以导出到到客户端 4.使用 ...

  4. C#+Aspose.Cells 导出Excel及设置样式 (Webform/Winform)

    在项目中用到,特此记录下来,Aspose.Cells 不依赖机器装没有装EXCEL都可以导出,很方便.具体可以参考其他 http://www.aspose.com/docs/display/cells ...

  5. csharp: read excel using Aspose.Cells

    /// <summary> /// /// </summary> /// <param name="strFileName"></para ...

  6. Aspose.Cells相应操作及下载

    Aspose.Cells相应操作 1,上传 1.1 Workbook Workbook workBook = new Workbook(); 属性: 名称 值类型 说明 Colors Color[] ...

  7. 《ArcGIS Runtime SDK for Android开发笔记》——(15)、要素绘制Drawtools3.0工具DEMO

    1.前言 移动GIS项目开发中点线面的要素绘制及编辑是最常用的操作,在ArcGIS Runtime SDK for iOS 自带AGSSketchLayer类可以帮助用户快速实现要素的绘制,图形编辑. ...

  8. C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]

    [csharp] view plain copy 1.添加引用: Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载.关于它的操作我在“Aspose.Cells操作说明 中文版 下载 ...

  9. 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能

    我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据, ...

随机推荐

  1. 请写一个C函数,判断处理器是大端存储还是小端存储,若处理器是Big_endian的,则返回0;若是Little_endian的,则返回1

    [解答] int checkCPU() { { union w { int a; char b; }c; c.a=1; return (c.b==1); } } [剖析] 嵌入式系统开发者应该对Lit ...

  2. Win7下Solr4.10.1和IK Analyzer中文分词

    1.下载IK中文分词压缩包IK Analyzer 2012FF_hf1,并解压到D:\IK Analyzer 2012FF_hf1: 2.将D:\IK Analyzer 2012FF_hf1\IKAn ...

  3. CentOS 添加/绑定 IP

    美国VPS的独立IP相对于国内而言,是非常的便宜的.比如有些美国VPS,买5个独立IP才三美元左右一个月.当我们购买了多个独立IP时,如果你不想再联系客服而漫长的等待,那就自己手动配置吧. 一.进入/ ...

  4. HttpHelper工具类

    /// <summary> /// 类说明:HttpHelper类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理 /// 重要提 ...

  5. javaWeb图片验证码代码

    1. [代码]初始粗糙代码 import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.a ...

  6. PCB设计备忘录

    在PCB设计过程中,常常有很多细节只有在实践中才能体会到其重要性,本人记性不好,索性把相关的注意点记录下来,也顺便希望能够给读者朋友们一些帮助. 接插件以及连接器比较常用的针脚之间间距有2.54mm/ ...

  7. AltiumDesigner14板框定义及3D显示图文教程

    很多人都跟我反应说AD14不能定义板框大小,或者说是不知道怎么定义板框的大小,确实AD14的操作和AD13或者是AD10的版本斗有一些差异,尤其是现在最新的版本AltiumDesigner14.3.1 ...

  8. 2个Web上传组件

    http://www.uploadify.com/download/ http://gmupload.tanjun.com.cn/

  9. GS1011无线模块的使用简介。

    一.硬件说明: 只是用电脑的串口助手与之通信,利用了max232进行电平转换.是用模块的UART0作为通信接口. 模块引脚 接点 说明   1.17.32.48 GND 模块地   9 3.3V VB ...

  10. sigaction函数的使用

    sigaction函数的功能是检查或修改与指定信号相关联的处理动作(可同时两种操作). 他是POSIX的信号接口,而signal()是标准C的信号接口(如果程序必须在非POSIX系统上运行,那么就应该 ...