Ionic2+WebApi 导出Excel转Pdf文件。
步骤:
1.首先在WebApi中先把excel生成好。
2.把excel转成Pdf,并返回下载的链接。
3.Ionic2的页面做好下载的接口。
嗯~思路很清晰,那么下面就来详细的操作吧。
以下是H5的页面效果图,最终导出的pdf也是如此。
The First Step
一、把 数据字典转成 excel
以下是数据的结构
/// <summary>
/// 统计分析
/// </summary>
public class AnalysisResultToTable
{
public string XZQMC { get; set; }
public List<ResultToYears> Data { get; set; }
}
public class ResultToYears
{
public string Year { get; set; }
public Quarter Quarters { get; set; }
}
/// <summary>
/// 季度
/// </summary>
public class Quarter
{
/// <summary>
/// 第一季度
/// </summary>
public string FirstQuarter { get; set; }
/// <summary>
/// 第二季度
/// </summary>
public string TwoQuarter { get; set; }
/// <summary>
/// 第三季度
/// </summary>
public string ThreeQuarter { get; set; }
/// <summary>
/// 第四季度
/// </summary>
public string FourQuarter { get; set; }
}
1)这个是别人写好的服务,拿到这个数据。
maps = service.DB_Statistic_ReportXZMJ(year, mjdw, createman, area);
2)接着这个就是我做的需要把maps里的数据转成excel 。
file = service.DB_Statistic_Report_Export(detailName, year, maps);
以下是DB_Statistic_Report_Export的具体代码包括设计excel的样式
/// <summary>
/// 统计分析导出--表
/// </summary>
/// <param name="year">年份</param>
/// <param name="mjdw">面积单位</param>
/// <param name="createman">登录名(如果用户名为空,表示查询区域)</param>
/// <param name="area">区域</param>
/// <returns></returns>
public Stream DB_Statistic_Report_Export(string type,string year, Dictionary<string, AnalysisResultToTable> dic)
{
var headName = "临沂市" + type;
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
ISheet sheet = hssfworkbook.CreateSheet(headName);
IRow row0 = sheet.CreateRow(0);
//样式一
var style= ExcelHelper.SetCellStyle(hssfworkbook, BorderStyle.Thin, HSSFColor.RoyalBlue.Index, HSSFColor.White.Index);
//样式二
var style1 = ExcelHelper.SetCellStyle(hssfworkbook, BorderStyle.Thin,HSSFColor.DarkTeal.Index,HSSFColor.White.Index);
//样式三
ICellStyle style2 = hssfworkbook.CreateCellStyle();
//水平对齐居中
style2.Alignment = HorizontalAlignment.Center;//水平对齐居中
style2.VerticalAlignment = VerticalAlignment.Center;//垂直居中
//标题
var yearArray = year.Split(',');
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 4 * yearArray.Length));
ICell cell = row0.CreateCell(0);
cell.SetCellValue(headName);
cell.CellStyle = style2;
//第二行 年份 第三行 季度
IRow row1 = sheet.CreateRow(1);
IRow row2 = sheet.CreateRow(2);
for (int i = 0; i < yearArray.Length; i++)
{
ICell cell1 = row1.CreateCell(4 * i + 1);
cell1.SetCellValue(yearArray[i]);
sheet.AddMergedRegion(new CellRangeAddress(1, 1, 4 * i + 1, 4 * (i + 1)));
cell1.CellStyle = style;
ICell cell2_1 = row2.CreateCell(i * 4 + 1);
cell2_1.SetCellValue("第一季度");
ICell cell2_2 = row2.CreateCell(i * 4 + 2);
cell2_2.SetCellValue("第二季度");
ICell cell2_3 = row2.CreateCell(i * 4 + 3);
cell2_3.SetCellValue("第三季度");
ICell cell2_4 = row2.CreateCell(i * 4 + 4);
cell2_4.SetCellValue("第四季度");
cell2_1.CellStyle = style1;
cell2_2.CellStyle = style1;
cell2_3.CellStyle = style1;
cell2_4.CellStyle = style1;
}
//第二行和第三行的第一列合并 为临沂市
ICell cell0_1 = row1.CreateCell(0);
cell0_1.SetCellValue("临沂市");
sheet.AddMergedRegion(new CellRangeAddress(1, 2, 0, 0));
cell0_1.CellStyle = style1;
//数据列表
int j = 1;
foreach (string key in dic.Keys)
{
IRow row = sheet.CreateRow(2 + j);
//区县名
ICell celli_0 = row.CreateCell(0);
celli_0.SetCellValue(dic[key].XZQMC);
for (int i = 0; i < yearArray.Length; i++)
{
var Data = dic[key].Data.OrderBy(p => p.Year).ToList();
//4个季度
ICell celli_1 = row.CreateCell(4 * i + 1);
celli_1.SetCellValue(Data[i].Quarters.FirstQuarter);
ICell celli_2 = row.CreateCell(4 * i + 2);
celli_2.SetCellValue(Data[i].Quarters.TwoQuarter);
ICell celli_3 = row.CreateCell(4 * i + 3);
celli_3.SetCellValue(Data[i].Quarters.ThreeQuarter);
ICell celli_4 = row.CreateCell(4 * i + 4);
celli_4.SetCellValue(Data[i].Quarters.FourQuarter);
if (j % 2 == 0)
{
celli_0.CellStyle = style1;
celli_1.CellStyle = style1;
celli_2.CellStyle = style1;
celli_3.CellStyle = style1;
celli_4.CellStyle = style1;
}
else
{
celli_0.CellStyle = style;
celli_1.CellStyle = style;
celli_2.CellStyle = style;
celli_3.CellStyle = style;
celli_4.CellStyle = style;
}
}
j++;
}
//转换为文件流
MemoryStream file = new MemoryStream();
hssfworkbook.Write(file);
file.Seek(0, SeekOrigin.Begin);
return file;
}
//给单元格设置样式的公共方法
/// <summary>
/// Excel帮助类
/// </summary>
public class ExcelHelper
{
/// <summary>
/// 设置单元格样式
/// </summary>
/// <param name="hssfworkbook">工作本</param>
/// <param name="borderStyle">边框样式</param>
/// <param name="borderColor">边框颜色</param>
/// <returns>ICellStyle</returns>
public static ICellStyle SetCellStyle(HSSFWorkbook hssfworkbook, BorderStyle borderStyle,short bgColor,short borderColor)
{
ICellStyle style = hssfworkbook.CreateCellStyle();
//背景颜色
style.FillForegroundColor = bgColor;
style.FillPattern = FillPattern.SolidForeground;
//水平对齐居中
style.Alignment = HorizontalAlignment.Center;//水平对齐居中
style.VerticalAlignment = VerticalAlignment.Center;//垂直居中
//边框及颜色
style.BorderBottom = borderStyle;
style.BorderLeft = borderStyle;
style.BorderRight = borderStyle;
style.BorderTop = borderStyle;
style.BottomBorderColor = borderColor;
style.LeftBorderColor = borderColor;
style.RightBorderColor = borderColor;
style.TopBorderColor = borderColor;
return style;
}
}
3) 然后把file存到服务器的本地文件中。
string name = year + "_" + detailName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");//xls文件名
using (FileStream fs = new FileStream(excelSavePath + name+".xls", FileMode.Create))//excelSavePath 服务器地址
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
file.Seek(0, SeekOrigin.Begin);
//参数:要写入到文件的数据数组,从数组的第几个开始写,一共写多少个字节
fs.Write(bytes, 0, bytes.Length);
}
以下是excel的效果
The Second Step
二、把 excel转成Pdf
ApiCommon.OfficeToPdf officeToPdf = new ApiCommon.OfficeToPdf();
officeToPdf.ConverterToPdf(excelSavePath + name + ".xls", pdfSavePath + name + ".pdf");
return ApiResult.Success(PDFPathDownload+name+".pdf");//返回服务器下载地址
以下是具体的excel转pdf代码
namespace Ionic_Server.ApiCommon
{
/// <summary>
/// 把 Office转成Pdf
/// </summary>
public class OfficeToPdf
{
/// <summary>
/// 转换excel 成PDF文档
/// </summary>
/// <param name="_lstrInputFile">原文件路径</param>
/// <param name="_lstrOutFile">pdf文件输出路径</param>
/// <returns>true 成功</returns>
public bool ConverterToPdf(string _lstrInputFile, string _lstrOutFile)
{
Microsoft.Office.Interop.Excel.Application lobjExcelApp = null;
Microsoft.Office.Interop.Excel.Workbooks lobjExcelWorkBooks = null;
Microsoft.Office.Interop.Excel.Workbook lobjExcelWorkBook = null;
string lstrTemp = string.Empty;
object lobjMissing = System.Reflection.Missing.Value;
try
{
lobjExcelApp = new Microsoft.Office.Interop.Excel.Application();
lobjExcelApp.Visible = true;
lobjExcelWorkBooks = lobjExcelApp.Workbooks;
lobjExcelWorkBook = lobjExcelWorkBooks.Open(_lstrInputFile, true, true, lobjMissing, lobjMissing, lobjMissing, true,
lobjMissing, lobjMissing, lobjMissing, lobjMissing, lobjMissing, false, lobjMissing, lobjMissing);
//Microsoft.Office.Interop.Excel 12.0.0.0之后才有这函数
lstrTemp = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".xls" + (lobjExcelWorkBook.HasVBProject ? 'm' : 'x');
//lstrTemp = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".xls";
//pdf临时存放路径
string pdfSavePath = System.Configuration.ConfigurationManager.AppSettings["TempPath"];
lobjExcelWorkBook.SaveAs(@pdfSavePath+ Guid.NewGuid().ToString() + ".xls");
//lobjExcelWorkBook.SaveAs(lstrTemp, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel4Workbook, Type.Missing, Type.Missing, Type.Missing, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
// false, Type.Missing, Type.Missing, Type.Missing);
//输出为PDF 第一个选项指定转出为PDF,还可以指定为XPS格式
lobjExcelWorkBook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, _lstrOutFile, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard, Type.Missing, false, Type.Missing, Type.Missing, false, Type.Missing);
lobjExcelWorkBooks.Close();
lobjExcelApp.Quit();
lobjExcelApp = null;
GC.Collect();//垃圾回收
}
catch (Exception ex)
{
LogAPI.Error(ex);
return false;
}
return true;
}
}
}
以下是pdf的效果
The Third Step
三、在Ionic的ts代码中写对应的下载代码
Ionic2+WebApi 导出Excel转Pdf文件。的更多相关文章
- 导出excel和PDF小结 vba
最近接触了一个关于Access工具的项目,所以整理下需要使用的方法. 功能要求简介: 1.将数据表中的数据导出到excel和PDF 2.并根据某个字段名称分sheet输出. 3.无模板方式 方案简介: ...
- Office系列---将Office文件(Word、PPT、Excel)转换为PDF文件,提取Office文件(Word、PPT)中的所有图片
将Office文件转换为PDF文件,提取Office文件中的所有图片 1.Office系列---将Office文件(Word.PPT.Excel)转换为PDF文件 1.1 基于Office实现的解决方 ...
- (转载)DBGridEh导出Excel等格式文件
DBGridEh导出Excel等格式文件 uses DBGridEhImpExp; {--------------------------------------------------------- ...
- BarTender 2016如何导出模板为pdf文件?
最近有小伙伴来问,BarTender 2016能不能导出模板为pdf文件?这个是可以的,之前针对BarTender 10.1就介绍过一种方法了.本文,小编再针对BarTender 2016给大家讲下如 ...
- 将页面中表格数据导出excel格式的文件(vue)
近期由于项目需要,需要将页面中的表格数据导出excel格式的文件,折腾了许久,在网上各种百度,虽然资料不少,但是大都不全,踩了许多坑,总算是皇天不负有心人,最后圆满解决了. 1.安装相关依赖(npm安 ...
- 【.Net 学习系列】-- 利用Aspose转换Excel为PDF文件
功能: 从数据库中查询出数据 利用Aspose.cell + Excel模板绑定数据源生成Excel文件 通过Aspose.pdf + 生成好的Excel生成PDF文件 实现: 查询数据,根据Exce ...
- java操作Excel、PDF文件
java操作Excel.PDF文件 分享者:Vashon 分享来源:CSDN博客 下面这些是在开发中用到的一些东西,有的代码贴的不是完整的,只是贴出了关于操作EXCEL的代码: jxl是一个*国人写的 ...
- jquery插件导出excel和pdf(解决中文乱码问题)
参考文件:http://jackyrong.iteye.com/blog/2169683 https://my.oschina.net/aruan/blog/418980 https://segmen ...
- 文件系统(01):基于SpringBoot框架,管理Excel和PDF文件类型
本文源码:GitHub·点这里 || GitEE·点这里 一.文档类型简介 1.Excel文档 Excel一款电子表格软件.直观的界面.出色的计算功能和图表工具,在系统开发中,经常用来把数据转存到Ex ...
随机推荐
- sudo 取消密码
通常我们并不以root身份登录,但是当我们执行某些命令 (command)时需要用到root权限,我们通常都是用"sudo command"来执行command.由于使用Ubunt ...
- 学习笔记《Java多线程编程实战指南》四
JAVA线程同步机制 线程同步机制:是一套用于协调线程间的数据访问及活动的机制,该机制用于保障线程安全以及实现这些线程的共同目标.java平台提供的线程同步机制包括锁.volatile关键字.fina ...
- [转][C#]ImageHelper
{ internal static class ImageHelper { public static Bitmap CloneBitmap(Image source) { if (source == ...
- Linux系统编程——fcntl
fcntl可实现对指定文件描述符的各种操作,其函数原型如下: int fcntl(int fd, int cmd, ... /* arg */ ); 头文件 #include <unistd.h ...
- springmvc读取服务器磁盘图片,显示于前台页面
在项目中的config目录下有一个文件,在后台程序中获取 它并使用. springmvc提供一个方法:File file = new ClassPathResource("NonTaxVou ...
- 卷积神经网络总结CNN【转载】
卷积神经网络CNN总结 从神经网络到卷积神经网络(CNN)我们知道神经网络的结构是这样的: 那卷积神经网络跟它是什么关系呢?其实卷积神经网络依旧是层级网络,只是层的功能和形式做了变化,可以说是传统神经 ...
- 微信小程序web-view的简单思考和实践
微信小程序的组件web-view推出有一段时间了,这个组件的推出可以说是微信小程序开发的一个重要事件,让微信小程序不会只束缚在微信圈子里了,打开了一个口子,这个口子或许还比较小,但未来有无限可能. 简 ...
- jenkins+maven+git持续集成部署问题总结
1.这个问题出现在单独构建一个项目的子模块时,很奇怪,构建父项目没有问题. Executing Maven: -N -B -f /root/.jenkins/workspace/newMer/pom. ...
- leetcode每日刷题计划-简单篇day8
今天是纠结要不要新买手机的一天QAQ想了想还是算了吧,等自己赚钱买,加油 Num 70 爬楼梯 Climbing Stairs class Solution { public: int climbSt ...
- LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...