C#使用Aspose.Cells导出Excel简单实现

首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net

将DataTable导出Xlsx格式的文件下载(网页输出):

 1 /// <summary>
 2 /// 导出Excel表格
 3 /// </summary>
 4 /// <param name="list">数据集合</param>
 5 /// <param name="header">数据表头</param>
 6 /// <returns></returns>
 7 public void ExportExcel(DataTable dt, string[] header)
 8 {
 9     Workbook wb = new Workbook(FileFormatType.Xlsx);
10     try
11     {
12         Worksheet sheet = wb.Worksheets[0];
13         sheet.Name = "MO上行查询结果";
14         if (dt.Rows.Count <= 0)
15         {
16             System.Web.HttpContext.Current.Response.Write("<script>alert(‘没有检测到需要导出数据!‘);</script>");
17             return;
18         }
19         // 为单元格添加样式
20         Aspose.Cells.Style style = wb.CreateStyle();
21         style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;  //设置居中
22         style.Font.Size = 12;//文字大小
23         style.Font.IsBold = true;//粗体
24         style.HorizontalAlignment = TextAlignmentType.Center;//文字居中
25
26         int rowIndex = 0;
27         for (int i = 0; i < header.Length; i++)
28         {
29             sheet.Cells[rowIndex, i].PutValue(header[i]);
30             sheet.Cells[rowIndex, i].SetStyle(style);
31             sheet.Cells.SetColumnWidth(i, 20);//设置宽度
32         }
33         for (int i = 0; i < dt.Rows.Count; i++)//遍历DataTable行
34         {
35             sheet.Cells[i + 1, 0].PutValue(dt.Rows[i]["SENDER"].ToString());
36             sheet.Cells[i + 1, 1].PutValue(dt.Rows[i]["SENDCONTENT"].ToString());
37             sheet.Cells[i + 1, 2].PutValue("");
38             sheet.Cells[i + 1, 3].PutValue(dt.Rows[i]["RECDATE"].ToString());
39             sheet.Cells[i + 1, 4].PutValue(dt.Rows[i]["sn"].ToString());
40         }
41     }
42     catch (Exception e)
43     {
44         System.Web.HttpContext.Current.Response.Write("<script>alert(‘导出异常:" + e.Message + "!‘);</script>");
45     }
46     #region 输出到Excel
47     using (MemoryStream ms = new MemoryStream())
48     {
49
50         wb.Save(ms, new OoxmlSaveOptions(SaveFormat.Xlsx));//默认支持xls版,需要修改指定版本
51         System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyyMMddHHmmssfff")));
52         System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
53         System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
54         wb = null;
55         System.Web.HttpContext.Current.Response.End();
56     }
57     #endregion
58 }

Aspose.Cells.dll 下载地址:http://pan.baidu.com/s/1o8TRXDg

其它相关参考:

https://my.oschina.net/u/876556/blog/98801

http://www.cnblogs.com/top5/archive/2010/02/16/1668801.html

http://www.cnblogs.com/springyangwc/archive/2011/08/12/2136498.html

原文地址:https://www.cnblogs.com/wwwbdabc/p/11652449.html

时间: 2024-08-29 13:19:57

C#使用Aspose.Cells导出Excel简单实现的相关文章

Aspose.cells 导出Excel

//Export private void btnExport_Click(object sender, RoutedEventArgs e) { string strDataTime = System.DateTime.Now.ToString("yyyyMMdd_HHmmss"); string strFileName = "buyf_Template" + strDataTime; SaveFileDialog save = new SaveFileDialo

Aspose.Cells导出excel

利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public static bool DataTableToExcel2(DataTable datatable, string filepath, out string error) { error = ""; Aspose.Cells.Workbook wb = new Aspose.Cells.W

extjs+Aspose.Cells导出到Excel

1.&=DataSource.Field,&=[DataSource].[Field]是对DataTable和几何类型的引用,将会从当前行开始竖直向下生成多行数据. 2.&=$data:是对变量或数组的引用.数组存在skip,horizontal等属性,具体参见官方网站 3.&=&=动态公式计算:{r}当前行,{c}当前列,{-n},{n}当前行或列的偏移量前n或后n. 4.&==是动态计算,如excel,if等语句.(if(logic_test,true_

Aspose.Cells 操作Excel相关文章

Aspose.Cells 根据Excel模板导出数据统计 使用Aspose.Cell控件实现Excel高难度报表的生成(一)

使用Aspose.Cells生成Excel的方法详解(转)

using System; using System.Collections.Generic;  using System.Linq;  using System.Web;  using System.IO;  using System.Data;  using Aspose.Cells;   /// <summary> ///OutFileDao 的摘要说明 /// </summary>  public class OutFileDao  {          public Ou

Aspose.cells 处理Excel文件

//浏览Excel文件 private void btnScan_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.DefaultExt = "xlsx"; ofd.Filter = "Excel file|*.xlsx"; if (ofd.ShowDialog() == true) { this.txtFilePath.Text = of

Excel导出学习之道:Java Web利用POI导出Excel简单例子

采用Spring mvc架构: Controller层代码如下 [java] view plaincopy @Controller public class StudentExportController{ @Autowired private StudentExportService studentExportService; @RequestMapping(value = "/excel/export") public void exportExcel(HttpServletReq

报表中的Excel操作之Aspose.Cells(Excel模板)

本文转载:http://www.cnblogs.com/whitewolf/archive/2011/03/21/Aspose_Cells_Template1.html 报表中的Excel操作之Aspose.Cells(Excel模板)

Aspose.Cells将Excel转成pdf

Aspose.Cells Excel转成pdf /// <summary> /// excel转pdf /// </summary> /// <param name="path">文件地址</param> /// <param name="newFilePath">转换后的文件地址</param> /// <returns></returns> public static