注意 aspose合并单元格后设置单元格样式要一格一格的设置

public class InvoiceAsposeExcel
{
/// <summary>
/// 导出数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="invoiceCno">结算单号</param>
/// <param name="facilitator">服务商</param>
/// <param name="cinput">制单人</param>
/// <param name="charges">下家付款费用</param>
/// <param name="upAllData">上家详细数据,获取时间</param>
/// <param name="currency">币种数据</param>
/// <param name="clientdt">客户统计数据</param>
/// <param name="dt">明细统计数据</param>
public void ProcessData(string path, string invoiceCno, string facilitator, string cinput, List<nextHomeCharges> charges, List<RatherCostModel> upAllData, List<op_sys_dic> currency, DataTable clientdt, DataTable dt)
{
try
{
Workbook workbook = new Workbook();
workbook.Worksheets.Clear();//工作副清除
workbook.Worksheets.Add("结算单");//sheet的名称
int startRow = 0;//写入数据开始行的下标
Worksheet wsData = workbook.Worksheets[0];//工作副本

#region 表头
SetTitlt(workbook, wsData, "Times New Roman", startRow, 0, "SHENZHEN TOPWAY BAISHUNDA CARGO FORWARDING INTERNATIONAL CO.,LTD.", dt.Columns.Count, 14, true, 17, true, 0);
#endregion

wsData.AutoFitRows();

workbook.Save(path);
if (DevExpress.XtraEditors.XtraMessageBox.Show("保存成功,是否打开文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
System.Diagnostics.Process.Start(path);//打开指定路径下的文件
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

/// <summary>
/// 合并单元格
/// </summary>
private void SetPutValue(Worksheet sheet, Style style, int startRow, int c, int column, string value, bool isCenter = false, bool isBold = false, bool isGroundColor = false, bool isFontColor = false)
{
SetStyle(sheet, startRow, c, column, style, isCenter, isBold, isGroundColor, isFontColor);
PutValue(sheet, style, startRow, c, value, column);
}
/// <summary>
/// 默认不合并单元格
/// </summary>
private void SetDefaultPutValue(Worksheet sheet, Style style, int startRow, int c, string value, bool isCenter = false, bool isBold = false, bool isGroundColor = false, bool isFontColor = false)
{
SetStyle(style, isCenter, isBold, isGroundColor, isFontColor);
PutValue(sheet, style, startRow, c, value, 0);
}
/// <summary>
/// 下家数据填充
/// </summary>
private void PutValue(Worksheet sheet, Style style, int startRow, int c, string name, int column)
{
sheet.Cells[startRow, c].PutValue(name);
sheet.Cells[startRow, c].SetStyle(style);
for (int i = 1; i < column; i++)
{
sheet.Cells[startRow, c + i].SetStyle(style);
}
}

private void SetClientCell(int startRow, Workbook workbook, Worksheet sheet, DataTable dt)
{
Style style = workbook.Styles[workbook.Styles.Add()];//新增样式
int c = 0;
for (int row = 0; row < dt.Rows.Count; row++)
{
c = 0;
bool isData = false;
for (int column = 0; column < dt.Columns.Count; column++)
{
if (isData)
{
SetStyle(sheet, row + startRow, c, 2, style, false, false, isData, isData);
isData = false;
}
else
{
isData = ExistsData(dt.Rows[row][column].ToString());
SetStyle(sheet, row + startRow, c, 2, style, false, false, isData, isData);
}
sheet.Cells[row + startRow, c].PutValue(dt.Rows[row][column].ToString());
sheet.Cells[row + startRow, c].SetStyle(style);
sheet.Cells[row + startRow, c + 1].SetStyle(style);//空数据设置样式
style = workbook.Styles[workbook.Styles.Add()];//新增样式
c = c + 2;
}
}
}

/// <summary>
/// 明细统计数据
/// </summary>
private void SetCell(int startRow, Workbook workbook, Worksheet sheet, DataTable dt)
{
Style style = workbook.Styles[workbook.Styles.Add()];//新增样式
SetStyle(style, true, true);
//生成字段名称
for (int i = 0; i < dt.Columns.Count; i++)
{
sheet.Cells[startRow, i].PutValue(dt.Columns[i].ToString());
sheet.Cells[startRow, i].SetStyle(style);
sheet.Cells.SetColumnWidth(i, 8.3);//设置列宽
}
//填充数据
SetStyle(style, false, false);
for (int row = 0; row < dt.Rows.Count; row++)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
sheet.Cells[row + startRow + 1, column].PutValue(dt.Rows[row][column].ToString());
sheet.Cells[row + startRow + 1, column].SetStyle(style);
}
}
//sheet.AutoFitColumns();
}

#region 结尾数据
private void SetEndData(int startRow, Workbook workbook, Worksheet sheet, string cinput)
{
Style style = workbook.Styles[workbook.Styles.Add()];//新增样式
int c = 0;
SetPutValue(sheet, style, startRow, c, 2, "制单:" + cinput, false, true);
c = c + 2;
SetPutValue(sheet, style, startRow, c, 2, "初审:", false, true);
c = c + 2;
SetPutValue(sheet, style, startRow, c, 3, "外围结算:", false, true);
c = c + 3;
SetPutValue(sheet, style, startRow, c, 3, "审核:", false, true);
c = c + 3;
SetPutValue(sheet, style, startRow, c, 3, "出纳:", false, true);
c = c + 3;
SetPutValue(sheet, style, startRow, c, 3, "会计:", false, true);

startRow++; c = 0;
SetPutValue(sheet, style, startRow, c, 2, DateTime.Now.ToString("yyy.MM.dd"), false, true);
c = c + 2;
SetPutValue(sheet, style, startRow, c, 2, "", false, true);
c = c + 2;
SetEmpty(sheet, style, startRow, 4, c);

startRow++; c = 0;
SetData(sheet, style, startRow);

startRow++; c = 0;
SetEmpty(sheet, style, startRow, 5, c);
}
/// <summary>
/// 数据为空
/// </summary>
private void SetEmpty(Worksheet sheet, Style style, int startRow, int j, int c)
{
for (int i = 0; i < j; i++)
{
if (i == 4)
SetPutValue(sheet, style, startRow, c, 4, "", false, true);
else
SetPutValue(sheet, style, startRow, c, 3, "", false, true);
c = c + 3;
}
}
/// <summary>
/// 设置数据
/// </summary>
private void SetData(Worksheet sheet, Style style, int startRow, int c = 0)
{
List<string> detail = new List<string>() { "复审:", "BSD结算:", "终审:", "主管审核:", "总经理审核:" };
for (int i = 0; i < detail.Count; i++)
{
if (i == 4)
SetPutValue(sheet, style, startRow, c, 4, detail[i], false, true);
else
SetPutValue(sheet, style, startRow, c, 3, detail[i], false, true);
c = c + 3;
}
}
#endregion

#region 样式
/// <summary>
/// 设置标题
/// </summary>
/// <param name="workbook">工作本</param>
/// <param name="sheet">工作簿</param>
/// <param name="fontName">字体名称</param>
/// <param name="startRow">行</param>
/// <param name="column">列</param>
/// <param name="titlt">文字</param>
/// <param name="columnsCount">合并的列的长度</param>
/// <param name="size">字体大小</param>
/// <param name="isBold">是否加粗</param>
/// <param name="rowHeight">行高</param>
private void SetTitlt(Workbook workbook, Worksheet sheet, string fontName, int startRow, int column, string titlt, int columnsCount, int size, bool isBold, double rowHeight, bool isAlignment = true, double columnWidth = 0)
{
Style style = workbook.Styles[workbook.Styles.Add()];//新增样式
if (isAlignment)
style.HorizontalAlignment = TextAlignmentType.Center;//文字居中
else
style.HorizontalAlignment = TextAlignmentType.Left;//文字居中
style.Font.Name = fontName;
style.Font.Size = size;
style.Font.IsBold = isBold;
sheet.Cells.Merge(startRow, column, 1, columnsCount);//合并单元格
sheet.Cells.SetRowHeight(0, rowHeight);//设置行高
if (columnWidth > 0)
{
sheet.Cells.SetColumnWidth(0, columnWidth);//设置列宽
//sheet.Cells.SetColumnWidthInch(0, columnWidth);//设置列宽
}
style.IsTextWrapped = true;
sheet.Cells[startRow, column].PutValue(titlt);//添加内容
sheet.Cells[startRow, column].SetStyle(style);
}

private void SetStyle(Worksheet sheet, int startRow, int column, int columnsCount, Style style, bool isCenter, bool isBold, bool isGroundColor = false, bool isFontColor = false)
{
SetStyle(style, isCenter, isBold, isGroundColor, isFontColor);
sheet.Cells.Merge(startRow, column, 1, columnsCount);//合并单元格
}

/// <summary>
/// 数据样式
/// </summary>
private void SetStyle(Style style, bool isCenter, bool isBold, bool isGroundColor = false, bool isFontColor = false)
{
style.Pattern = Aspose.Cells.BackgroundType.Solid;//边框
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;//应用边界线 左边界线
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;//应用边界线 上边界线
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;//应用边界线 下边界线
//style.Borders[BorderType.BottomBorder].Color = System.Drawing.Color.Black;
if (isCenter)
style.HorizontalAlignment = TextAlignmentType.Center;//文字居中
else
style.HorizontalAlignment = TextAlignmentType.Left;
style.Font.Name = "宋体";
style.Font.Size = 9;
style.Font.IsBold = isBold;
style.IsTextWrapped = true;
if (isGroundColor)
style.ForegroundColor = System.Drawing.Color.FromArgb(255, 255, 0);//设置背景色 可以参考颜色代码对照表
//else
//{
// style.ForegroundColor = System.Drawing.Color.FromArgb(255, 255, 255);//设置背景色 可以参考颜色代码对照表
//}
if (isFontColor)
style.Font.Color = Color.Red;
//else
// style.Font.Color = Color.Black;
}
#endregion
}

导入excel变DataTable

//路径    列名
protected DataTable ReadExcel(string filename, bool HDR = true)
{
Workbook workbook = new Workbook(filename);
Worksheet worksheet = workbook.Worksheets[];
if (HDR)
return worksheet.Cells.ExportDataTableAsString(, , worksheet.Cells.MaxDataRow + , worksheet.Cells.MaxDataColumn + , true);
else
return worksheet.Cells.ExportDataTableAsString(, , worksheet.Cells.MaxDataRow + , worksheet.Cells.MaxDataColumn + );
}

aspose导出数据的更多相关文章

  1. Aspose 导出excel小demo

    //转为pdf         private void CelltoPDF(string cellPath, string pdfPath)         {             Workbo ...

  2. 通过Workbook类 生成Excel导出数据

    需求: 实现错误信息生成Excel保存到本地让用户查看. 刚开始使用了微软自带的Microsoft.Office.Interop.Excel类库. Microsoft.Office.Interop.E ...

  3. CRL快速开发框架系列教程九(导入/导出数据)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  4. Vertica 导出数据测试用例

    需求:构建简单的测试用例,完成演示Vertica导出数据的功能. 测试用例:导出test业务用户t_jingyu表中的数据. 一.初始化测试环境 二.导出数据 2.1 vsql命令说明帮助 2.2 导 ...

  5. 1.ASP.NET MVC使用EPPlus,导出数据到Excel中

    好久没写博客了,今天特地来更新一下,今天我们要学习的是如何导出数据到Excel文件中,这里我使用的是免费开源的Epplus组件. 源代码下载:https://github.com/caofangshe ...

  6. 导出数据到Excel --使用ExcelReport有感

    先看图,这是几个月前用NPOI写的导出数据到Excel,用了上百行代码,而且难控制,导出来也比较难看 excel打开的效果 下面是我用ExcelReport类库导出到Excel的操作 1.首先引用Ex ...

  7. MySQL 导出数据

    MySQL中你可以使用SELECT...INTO OUTFILE语句来简单的导出数据到文本文件上. 使用 SELECT ... INTO OUTFILE 语句导出数据 以下实例中我们将数据表 cnbl ...

  8. 使用Open xml 操作Excel系列之二--从data table导出数据到Excel

    由于Excel中提供了透视表PivotTable,许多项目都使用它来作为数据分析报表. 在有些情况下,我们需要在Excel中设计好模板,包括数据源表,透视表等, 当数据导入到数据源表时,自动更新透视表 ...

  9. Dynamics CRM导出数据到Excel

    原创地址:http://www.cnblogs.com/jfzhu/p/4276212.html 转载请注明出处 Pivot Table是微软BI的一个重要工具,所以这里讲一下Dynamics CRM ...

随机推荐

  1. 在CSS中水平居中和垂直居中:完整的指南

    这篇文章将会按照如下思路展开: 一.水平居中 1. 行内元素水平居中 2. block元素水平居中 3. 多个块级元素水平居中 二.垂直居中 1. 行内元素水平居中 2. block元素水平居中 3. ...

  2. Qt DLL总结【一】-链接库预备知识

    1.链接库概念 静态链接库和动态链接库介绍 我们可以创建一种文件里面包含了很多函数和变量的目标代码,链接的时候只要把这个文件指示给链接程序就自动地从文件中查找符合要求的函数和变量进行链接,整个查找过程 ...

  3. AB窗体互传参数本质

    一.找了好几个,都不靠谱,不是说不靠谱,自己感觉太繁琐,根本就是本窗体的属性(对象)的传递,1实例化2把实例化后的窗体属性=本窗体的对象 二.传递的的时候都是在互相引用的时候传递,推荐的个人认为最简单 ...

  4. 在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型

    1)  在默认情况下,springmvc不能将String类型转成java.util.Date类型,所有我们只能在Action 中自定义类型转换器 <form action="${pa ...

  5. js 操作select和option常见用法

    1.获取选中select的value和text,html <select id="mySelect"> <option value="1"&g ...

  6. Node.JS数组及For 语句

    for Each语句: var arr = ["Zhang San", "Li Si", "Wang Wu"] arr.forEach(fu ...

  7. 终端less命令执行完之后怎样退出

    Linux中的less命令主要用来浏览文件内容,与more命令的用法相似,不同于more命令的是,less命令可往回卷动浏览以看过的部分,less 的用法比起 more 更加的有弹性.如果想退出les ...

  8. Linux (Ubuntu)安装svn

    1 先查看是否已经安装了svn 如果没有安装svn则: ubuntu@ip----:~$ svn --version The program 'svn' is currently not instal ...

  9. C2B电商三种主要模式的分析_数据分析师

    C2B电商三种主要模式的分析_数据分析师 在过去的一年中电商领域血雨腥风,尤其是天猫.京东.苏宁.当当.易讯等B2C电商打得不亦乐乎.而随着B2C领域竞争进入白热化阶段,C2B模式也在天猫" ...

  10. three.js后期之自定义shader通道实现扫光效果

    如果你还不知道如何在three.js中添加后期渲染通道,请先看一下官方的一个最简单的demo : github. 正如demo中所示的那样,我们的扫光效果,也是一个自定义的ShaderPass. 所以 ...