在项目中用到,特此记录下来,Aspose.Cells 不依赖机器装没有装EXCEL都可以导出,很方便。具体可以参考其他

http://www.aspose.com/docs/display/cellsnet/Importing+Data+to+Worksheets#ImportingDatatoWorksheets-array

Webfrom版本:

效果如下:

  1. protected void btnAsnExport_ServerClick(object sender, EventArgs e)
  2. {
  3. var getAsnData = SearchDataClass.GetAsnSearchData(txtAsnNo.Value,
  4. hfCustomerID.Value, txtTimeSelect.Value,txtSku.Value,txtSkuContent.Value);
  5. //设置导出excel列的标题
  6. ArrayList ColTitle = new ArrayList()
  7. { "ASN编号", "SKU", "产品描述", "预期数量", "收货数量",
  8. "单位","收货库位","收货时间","所属客户","ASN状态","ASN创建时间" };
  9. //string[] strTitle = new string[] { "ASNNo", "SKU", "SKUDescrC", "ExpectedQty", "ReceivedQty", "UOM",
  10. "ReceivingLocation", "ReceivedTime", "CustomerID", "CodeName_C" };
  11. if (getAsnData.ToList().Count > 0)
  12. {
  13. Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
  14. //创建一个sheet
  15. Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
  16. //为单元格添加样式
  17. Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
  18. style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
  19. style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线
  20. style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线
  21. style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线
  22. style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线
  23. //给各列的标题行PutValue赋值
  24. int currow = 0;
  25. byte curcol = 0;
  26. //sheet.Cells.ImportCustomObjects((System.Collections.ICollection)getAsnData,
  27. //strTitle, true, 0, 0, getAsnData.Count, true, "yyyy/MM/dd HH:mm", false);
  28. sheet.Cells.ImportCustomObjects((System.Collections.ICollection)getAsnData,
  29. null, true, 0, 0, getAsnData.Count, true, "yyyy/MM/dd HH:mm", false);
  30. // 设置内容样式
  31. for (int i = 0; i < getAsnData.ToList().Count; i++)
  32. {
  33. for (int j = 0; j < 11; j++)
  34. {
  35. sheet.Cells[i + 1, j].Style = style;
  36. sheet.Cells[i + 1, 2].Style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Left;
  37. sheet.Cells[i + 1, 7].Style.Custom = "yyyy/MM/dd HH:mm";
  38. sheet.Cells[i + 1, 10].Style.Custom = "yyyy/MM/dd HH:mm";
  39. }
  40. }
  41. // 设置标题样式及背景色
  42. foreach (string s in ColTitle)
  43. {
  44. sheet.Cells[currow, curcol].PutValue(s);
  45. style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
  46. style.Pattern = Aspose.Cells.BackgroundType.Solid;
  47. style.Font.IsBold = true;
  48. sheet.Cells[currow, curcol].Style = style;
  49. curcol++;
  50. }
  51. Aspose.Cells.Cells cells = sheet.Cells;
  52. //设置标题行高
  53. cells.SetRowHeight(0, 30);
  54. //让各列自适应宽度
  55. sheet.AutoFitColumns();
  56. //生成数据流
  57. System.IO.MemoryStream ms = workbook.SaveToStream();
  58. byte[] bt = ms.ToArray();
  59. //客户端保存的文件名
  60. string fileName = "入库查询数据导出" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
  61. //以字符流的形式下载文件
  62. Response.ContentType = "application/vnd.ms-excel";
  63. //通知浏览器下载文件而不是打开
  64. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
  65. Response.BinaryWrite(bt);
  66. Response.Flush();
  67. Response.End();
  68. }
  69. }

Winform版本:

效果图如下:


    1. public void ExportExcelWithAspose(MDataTable dt, string fileName)
    2. {
    3. string saveFileName = "";
    4. SaveFileDialog saveDialog = new SaveFileDialog();
    5. saveDialog.DefaultExt = "xls";
    6. saveDialog.Filter = "Excel文件|*.xls";
    7. saveDialog.FileName = fileName;
    8. saveDialog.ShowDialog();
    9. saveFileName = saveDialog.FileName;
    10. if (saveFileName.IndexOf(":") < 0) return; //被点了取消
    11. Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    12. if (xlApp == null)
    13. {
    14. MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
    15. return;
    16. }
    17. try
    18. {
    19. Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
    20. Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
    21. Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0];
    22. Aspose.Cells.Cells cells = cellSheet.Cells ;//单元格
    23. //为单元格添加样式
    24. Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
    25. //设置居中
    26. style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
    27. //行索引
    28. int rowIndex = 0;
    29. //列索引
    30. int colIndex = 0;
    31. //列总数
    32. int colCount = dt.Columns.Count;
    33. //总行数
    34. int rowCount = dt.Rows.Count;
    35. rowIndex++;
    36. for (int i = 0; i < rowCount; i++)
    37. {
    38. colIndex = 0;
    39. for (int j = 0; j < colCount; j++)
    40. {
    41. if (j == 5) { cellSheet.Cells[rowIndex, colIndex].PutValue(Convert.ToDateTime (dt.Rows[i][j].Value).ToString("yyyy/MM/dd HH:mm:ss")); }
    42. else { cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].Value); }
    43. style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线
    44. style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线
    45. style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线
    46. style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线
    47. cellSheet.Cells[rowIndex, colIndex].Style = style;
    48. colIndex++;
    49. }
    50. rowIndex++;
    51. }
    52. //清除内容时行列索引值为0
    53. rowIndex = 0; colIndex = 0;
    54. //列名的处理
    55. for (int i = 0; i < colCount; i++)
    56. {
    57. cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName);
    58. //设置背景颜色
    59. style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
    60. style.Pattern = Aspose.Cells.BackgroundType.Solid;
    61. style.Font.IsBold = true;
    62. style.IsTextWrapped = true;
    63. cells.SetRowHeight(0, 38);//设置行高
    64. cellSheet.Cells[rowIndex, colIndex].Style = style;
    65. colIndex++;
    66. }
    67. cellSheet.AutoFitColumns();
    68. workbook.Save(Path.GetFullPath(saveFileName));
    69. xlApp.Quit();
    70. GC.Collect();//强行销毁
    71. MessageBox.Show("文件: " + fileName + ".xls 保存成功", "信息提示", MessageBoxButtons.OK,MessageBoxIcon.Information);
    72. }
    73. catch (Exception ex)
    74. {
    75. MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
    76. }
    77. }

C#+Aspose.Cells 导出Excel及设置样式 (Webform/Winform)的更多相关文章

  1. Aspose.Cells导出Excel(1)

    利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...

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

    首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...

  3. Aspose.Cells导出Excel(2)

    DataTable dtTitle = ds.Tables[]; DataTable dtDetail = ds.Tables[]; int columns = dtTitle.Columns.Cou ...

  4. aspose.Cells 导出Excel

    aspose aspse.Cells可以操作Excel,且不依赖于系统环境. 使用模板,通过绑定输出数据源 这种适合于对格式没有特别要求的,直接绑定数据源即可.和数据绑定控件差不多. Workbook ...

  5. C# 使用Aspose.Cells 导出Excel

    今天在工作中碰到同事用了一种新型的方式导入excel,在此做个学习记录. 插件:Aspose.Cells 第一步:准备好导出的模板,例子: C#代码: #region 验证数据 if (model = ...

  6. Aspose.Cells 导出 excel

    Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(); Aspose.Cells.Worksheet sheet = book.Worksh ...

  7. aspose.cell 给excel表格设置样式

    方法1: Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 styleTitle.HorizontalAlignment ...

  8. 使用Aspose.Cells读取Excel

      最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...

  9. 使用aspose.cell导出excel需要注意什么?

    1.如果导出的数据源是汇总出来的,最好方法是将数据源放到缓存里面,当基本数据源变化的时候,在改变数据2.使用模板导出EXCEL,这样很多样式可以在模板文件里面直接设置,例如:默认打开页签,让列头固定3 ...

随机推荐

  1. Apache Shiro 反序列化RCE漏洞

    漏洞介绍 漏洞类型 :JAVA反序列化(RCE) 影响版本 :Apache Shiro 1.2.4及其之前版本 漏洞评级 :高危 漏洞分析 #: 下载漏洞环境: git clone https://g ...

  2. github的README.md文件格式

    一.在线编辑器:stackedit 网址:https://stackedit.io/ README.md是使用Markdown语法.基本规则如下: Markdown 语法速查表 1 标题与文字格式 标 ...

  3. 基于spring-cloud的微服务(1) 服务注册中心eureka

    eureka是Netflix提供的服务注册中心组建,springcloud将其做了封装,作为自己的微服务架构中的一个注册中心组建 下面的例子在IDEA中启动一个eureka的实例,然后提供一个prov ...

  4. 23种设计模式之抽象工厂(Abstract Factory)

    抽象工厂模式又称为Kit模式,属于对象创建型模式.抽象工厂模式是所有形式的工厂模式中最为抽象和最具一般性的一种形态,它提供了一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类.在抽象工厂模 ...

  5. JVM源码分析之javaagent原理完全解读

    概述 本文重点讲述javaagent的具体实现,因为它面向的是我们Java程序员,而且agent都是用Java编写的,不需要太多的C/C++编程基础,不过这篇文章里也会讲到JVMTIAgent(C实现 ...

  6. android极光杀掉程序收不到通知

    http://docs.jpush.io/guideline/faq/#android 第三方系统收不到推送的消息 由于第三方 ROM 的管理软件需要用户手动操作 小米[MIUI] 自启动管理:需要把 ...

  7. DevOps之零停机部署

    “零停机部署(ZDD)可在不中断现有服务的情况下部署新版系统.” 通过ZDD方式部署应用程序时,可在确保用户不会遭遇应用程序停机的前提下将新版应用引入生产环境.从用户和公司的角度来看,这应该是最佳部署 ...

  8. 编译支持opengl的opencv

    opencv默认安装是不支持opengl的. 也就是如果调用一个支持opengl的窗口会报错,no opengl support ubuntu下安装opencv,支持opengl要在cmake的时候, ...

  9. ubuntu16.04下笔记本电脑扩展双屏安装过程

    想给笔记本电脑外界一个显示屏,因为科研需要,我的笔记本是windows10加Ubuntu16.04双系统,主要使用Ubuntu系统. 首先是硬件 一个外置显示屏是必须的了,然后我的笔电上只有HDMI接 ...

  10. 计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]

    题目链接:https://nanti.jisuanke.com/t/31453 After Incident, a feast is usually held in Hakurei Shrine. T ...