注意 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. Netfilter 之 钩子函数与钩子点关系图

    概述 通过钩子点和优先级的代码追溯,得到如下对应关系图,图中横坐标为钩子点,纵坐标为优先级,每个钩子点上的钩子函数按照优先级排布: 详细分析 5个钩子点如下所示,在这个五个钩子点上的钩子函数按照上面的 ...

  2. Flume-Spooling Directory Source 监控目录下多个新文件

    使用 Flume 监听整个目录的文件,并上传至 HDFS. 一.创建配置文件 flume-dir-hdfs.conf https://flume.apache.org/FlumeUserGuide.h ...

  3. pytorch-googleNet

    googleNet网络结构 输入网络: 由4个分支网络构成 第一分支: 由1x1的卷积构成 第二分支: 由1x1的卷积,3x3的卷积构成 第三分支: 由1x1的卷积, 5x5的卷积构成 第四分支: 由 ...

  4. goland 可用注册码(license)

    N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...

  5. SCM是什么?

    答: 全称为Software Configuration Management,即为软件配置管理

  6. Java访问Oracle服务器

    Java访问Oracle服务器--orcl数据库---emp表 private static String driver =         "oracle.jdbc.driver.Orac ...

  7. Flink初探wordCout

    知识点 Flink介绍 1.无界数据-->数据不断产生 2.有界数据-->最终不再改变的数据 3.有界数据集是无界数据集的一个特例 4.有界数据集在flink内部是以一种终态数据集进行处理 ...

  8. [spring mvc][转]<mvc:default-servlet-handler/>的作用

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  9. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-1.数据信息安全--微信授权一键登录功能介绍

    笔记 1.数据信息安全--微信授权一键登录功能介绍 简介:讲解登录方式优缺点和微信授权一键登录功能介绍         1.手机号或者邮箱注册             优点:              ...

  10. 用Python计算三角函数之acos()方法的使用

    用Python计算三角函数之acos()方法的使用 acos()方法返回x的反余弦值,以弧度表示. 语法 以下是acos()方法的语法:     acos(x) 注意:此函数是无法直接访问的,所以我们 ...