注意 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. XGBoost原理简介

    XGBoost是GBDT的改进和重要实现,主要在于: 提出稀疏感知(sparsity-aware)算法. 加权分位数快速近似学习算法. 缓存访问模式,数据压缩和分片上的实现上的改进. 加入了Shrin ...

  2. C++ UFT-8和GB2312间的转换

    在这个帖子找到的代码 还蛮好用的 https://bbs.csdn.net/topics/391040755 #include <codecvt> #include <locale& ...

  3. Linux终端Terminal常用快捷键

    快捷键 功能CTRL+ALT+T 打开终端Ctrl+a 光标移动到开始位置Ctrl+e 光标移动到最末尾Ctrl+k 删除此处至末尾的所有内容Ctrl+u 删除此处至开始的所有内容Ctrl+d 删除当 ...

  4. 开发-组件-GemBox:百科

    ylbtech-开发-组件-GemBox:百科 6000+ Companies Trust GemBox With .NET and Java File Format Components GemBo ...

  5. 在业务控制方法中写入包装User的模型来收集参数

    可以在业务控制方法中书写0个或多个模型来收集客户端的参数 1)  如果多个模型中有相同的属性时,可以用user.name或admin.name来收集客户端参数 2)  用一个新的模型将User和Adm ...

  6. 处理输入为非对角阵的Clustering by fast search and find of density peak代码

    Clustering by fast search and find of density peak. Alex Rodriguez, Alessandro Laio 是发表在Science上的一篇很 ...

  7. unix进程通信方式总结(中)(转)

    在上一篇博客http://blog.csdn.net/caoyan_12727/article/details/52049417已经总结了<<uinx环境高级编程>>进程通信前 ...

  8. IDEA使用git

    本文转自:http://www.cnblogs.com/java-maowei/p/5950930.html 一.安装git 下载地址:  https://git-scm.com/download/w ...

  9. syspolicy_purge_history作业故障排除

    描述 我们有一台数据库服务器windows 2012 r2 上有安装sql server 2012 和sql server 2016双实例,后续又把sql 2016的服务全部停用,即只保留sql 20 ...

  10. Driver drowsy detection dataset

    Introduction Driver drowsy detection dataset consists of both male and female drivers, with various ...