mvc导出数据到pdf
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
public ActionResult PdfExport(int y = , int m = , string departmentcd = "",string outFlag="")
{
System.Collections.ArrayList anonymousEntities = new System.Collections.ArrayList();
foreach (var item in db.WorkRecords.ToList())
{
WorkRecords workrecord = new WorkRecords();
workrecord.UserCD = item.UserCD;
workrecord.UserName = item.UserName;
anonymousEntities.Add(workrecord);
}
byte[] file = new byte[];
string depname = new BLL.DepartMentsBLL().GetDepartmentNameByCD(int.Parse(departmentcd));
string filename = depname + y.ToString() + "年" + m.ToString() + "月.pdf";
// filename = HttpUtility.UrlEncode(filename);
Document document = new Document(PageSize.A4, , , , );
//PdfWriter writer = PdfWriter.GetInstance(document, new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath("/material/pdfsample.pdf"), System.IO.FileMode.Create));
PdfWriter writer = PdfWriter.GetInstance(document, new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath("/PDF/pdfsample.pdf"), System.IO.FileMode.Create));
BaseFont bfChinese =iTextSharp.text.pdf.BaseFont.CreateFont(@"C:\WINDOWS\Fonts\MSMINCHO.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font fontChinese1 = new Font(bfChinese, );
Font ftitle = new Font(bfChinese, , Font.NORMAL, BaseColor.BLACK);
Font fontRukuHeader = new Font(bfChinese, , Font.NORMAL, BaseColor.BLACK);
Font footerChinese = new Font(bfChinese,, Font.NORMAL, BaseColor.BLACK);
Font footerChinese_3 = new Font(bfChinese, , Font.NORMAL, BaseColor.BLACK);
document.Open(); Paragraph title1 = new Paragraph("注意事項", ftitle);
title1.Alignment = Element.ALIGN_LEFT;
document.Add(title1);
Paragraph title2 = new Paragraph("申请", ftitle);
title2.Alignment = Element.ALIGN_CENTER;
document.Add(title2); document.NewPage();//创建下一页
PdfPTable table = new PdfPTable();
//Paragraph title3 = new Paragraph("一覧\n", ftitle);
//title3.Alignment = Element.ALIGN_LEFT;
//document.Add(title3);
Chunk ch = new Chunk("一覧");
ch.setLineHeight();
ch.Font = ftitle;
document.Add(ch); table.DefaultCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
table.DefaultCell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.WidthPercentage = ;
Font headerfont1 = new Font(bfChinese, , Font.BOLD, BaseColor.WHITE);
//table header
table.AddCell(makerCell("社員番号", headerfont1));
table.AddCell(makerCell("社員名", headerfont1));
table.AddCell(makerCell("時間外", headerfont1));
table.AddCell(makerCell("休日出勤", headerfont1));
table.AddCell(makerCell("祝日出勤", headerfont1));
table.AddCell(makerCell("総計", headerfont1)); string DepartmentCDs = "";
if (String.IsNullOrEmpty(departmentcd))
{
departmentcd = new NewWorkManager.BLL.UsersBLL().GetDepartmentCD(User.Identity.Name).ToString();
DepartmentCDs = departmentcd;
}
else
{
int intid = int.Parse(departmentcd);
var dep = db.Departments.Where(a => a.FatherDepartmentCD == intid).ToList();
foreach (var item in dep)
{
DepartmentCDs += item.DepartmentCD.ToString() + ",";
var smalldep = db.Departments.Where(a => a.FatherDepartmentCD == item.DepartmentCD).ToList();
foreach (var smallitem in smalldep)
{
DepartmentCDs += smallitem.DepartmentCD.ToString() + ",";
}
}
}
ViewBag.id = new SelectList(db.Departments.Where(a => a.IsActiveFlg == ), "DepartmentCD", "DepartmentName", departmentcd); string[] s = DepartmentCDs.Split(new char[] { ',' });
List<int> list = new List<int> { };
foreach (var a in s)
{
if (!String.IsNullOrEmpty(a))
{
list.Add(int.Parse(a));
}
}
list.Add(int.Parse(departmentcd));
var dbthisDepartment = from w in db.WorkRecords
where list.Contains(w.DepartmentCD)
select w; DateTime clearmonth = DateTime.Parse(y.ToString() + "/" + m.ToString() + "/1");
var dbapplication = dbthisDepartment.Where(a => a.ClearingMonth == clearmonth && a.ApproveFlg1 == && a.ApproveFlg2 == && (a.IsAdjustFlg == || (a.IsAdjustFlg == && a.WorkType == ))).ToList(); table.DefaultCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; foreach (var item in dbapplication.Select(a => a.UserCD).Distinct().ToList())
{
table.AddCell(new Phrase(item.ToString(), footerChinese));
table.AddCell(new Phrase(new NewWorkManager.BLL.UsersBLL().getRealNameByName(item).ToString(), footerChinese));
table.AddCell(new Phrase(dbapplication.Where(a => a.UserCD == item && a.WorkType == ).Select(a => a.WorkTimeTotalHour).Sum().ToString(), footerChinese));
table.AddCell(new Phrase(dbapplication.Where(a => a.UserCD == item && (a.WorkType == || (a.WorkType == && a.IsAdjustFlg == ))).Select(a => a.WorkTimeTotalHour).Sum().ToString(), footerChinese));
table.AddCell(new Phrase(dbapplication.Where(a => a.UserCD == item && a.WorkType == && a.IsAdjustFlg == ).Select(a => a.WorkTimeTotalHour).Sum().ToString(), footerChinese));
table.AddCell(new Phrase(dbapplication.Where(a => a.UserCD == item).Select(a => a.WorkTimeTotalHour).Sum().ToString(), footerChinese)); }
table.AddCell(new Phrase(""));
table.AddCell(new Phrase("合計",footerChinese));
table.AddCell(new Phrase(dbapplication.Where(a => a.WorkType == ).Select(a => a.WorkTimeTotalHour).Sum().ToString(),footerChinese));
table.AddCell(new Phrase(dbapplication.Where(a => (a.WorkType == || (a.WorkType == && a.IsAdjustFlg == ))).Select(a => a.WorkTimeTotalHour).Sum().ToString(),footerChinese));
table.AddCell(new Phrase(dbapplication.Where(a => a.WorkType == && a.IsAdjustFlg == ).Select(a => a.WorkTimeTotalHour).Sum().ToString(),footerChinese));
table.AddCell(new Phrase(dbapplication.Select(a => a.WorkTimeTotalHour).Sum().ToString(),footerChinese));
document.Add(table); document.Close(); string fullFileName = System.Web.HttpContext.Current.Server.MapPath("/PDF/pdfsample.pdf");
Byte[] btArray = WriteToPdf(fullFileName, DateTime.Now.ToString("yyyyMMddhhmmssff"), bfChinese);
//fullFileName = Server.MapPath("/material/pdfsample.pdf");
FileStream fs = new FileStream(fullFileName, FileMode.Create, FileAccess.Write);
fs.Write(btArray, , btArray.Length);
fs.Flush();
fs.Close();
//fullFileName = Server.MapPath("/material/pdfsample.pdf");
filename = depname + y.ToString() + "年" + m.ToString() + "月.pdf";
FileInfo downloadFile = new FileInfo(fullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
Response.AppendHeader("Content-Length", downloadFile.Length.ToString());
//System.Web.HttpContext.Current.Response.WriteFile(downloadFile.FullName);
Response.WriteFile(downloadFile.FullName);
Response.Flush();
Response.End();
return null; }
public PdfPCell makerCell(string str,Font fon){
PdfPCell cell=new PdfPCell(new Phrase(str,fon));
BaseColor bs = new BaseColor(System.Drawing.Color.FromArgb(, , ));
cell.BackgroundColor = bs;
return cell; }
public static byte[] WriteToPdf(string sourceFile, string stringToWriteToPdf, BaseFont bfChinese)
{
PdfReader reader = new PdfReader(sourceFile);
using (MemoryStream stream = new MemoryStream())
{
PdfStamper pdfstamper = new PdfStamper(reader, stream);
for (int i = ; i <= reader.NumberOfPages; i++)
{
Rectangle pageSize = reader.GetPageSizeWithRotation(i);
PdfContentByte pdfpageContents = pdfstamper.GetUnderContent(i);
pdfpageContents.BeginText();
pdfpageContents.SetFontAndSize(bfChinese, );
pdfpageContents.SetRGBColorFill(, , );
float textAngle = 45.0f;
pdfpageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, stringToWriteToPdf, pageSize.Width / , pageSize.Height / , textAngle);
pdfpageContents.EndText();
}
pdfstamper.FormFlattening = true;
pdfstamper.Close();
reader.Close();
return stream.ToArray();
}
}
参照:http://www.cnblogs.com/hfliyi/archive/2012/07/07/2580763.html
http://www.cnblogs.com/LifelongLearning/archive/2011/06/22/2086802.html
http://blog.csdn.net/aeolus1019/article/details/8208226
http://www.cnblogs.com/yangfan/archive/2008/05/23/1205976.html
mvc导出数据到pdf的更多相关文章
- MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult
导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...
- 1.ASP.NET MVC使用EPPlus,导出数据到Excel中
好久没写博客了,今天特地来更新一下,今天我们要学习的是如何导出数据到Excel文件中,这里我使用的是免费开源的Epplus组件. 源代码下载:https://github.com/caofangshe ...
- mvc导出excel 之 新
前段时间做的mvc导出excel 老大说要进行优化,我原来导出是用npoi插件进行导出,格式是将数据放入到datatable中,然后进行导出. 说要优化的时候就想着将datatable数据导出格式改为 ...
- (转)原始图像数据和PDF中的图像数据
比较原始图像数据和PDF中的图像数据,结果见表1.1.表1.1中各种“解码器”的解释见本文后续的“PDF支持的图像格式”部分,“PDF中的图像数据”各栏中的数据来自开源的PdfView.如果您有兴趣查 ...
- 导出excel和PDF小结 vba
最近接触了一个关于Access工具的项目,所以整理下需要使用的方法. 功能要求简介: 1.将数据表中的数据导出到excel和PDF 2.并根据某个字段名称分sheet输出. 3.无模板方式 方案简介: ...
- PLSQL_数据泵导入导出数据Impdp/ Expdp(概念)
2014-08-31 Created By BaoXinjian
- ASP.NET MVC导出excel
ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...
- ASP.NET MVC 导出Word报表
最近要做MVC导出Word报表功能.查了查资料发现一个好用的插件就是Aspose.Word.这个插件也很有名气,也很好用. 1.首先就是引用该插件 2.填充Word模版 3.后台操作 private ...
- 导出数据到Excel表格
开发工具与关键技术:Visual Studio 和 ASP.NET.MVC,作者:陈鸿鹏撰写时间:2019年5月25日123下面是我们来学习的导出数据到Excel表格的总结首先在视图层写导出数据的点击 ...
随机推荐
- 从Windows文件夹到Linux分区
1. 先说几句 如果你, 知道什么是Windows系统 简单使用Windows系统 想要了解或安装Linux 刚接触Linux会有以下疑惑, 什么是分区? 如何建立分区? 怎么安装系统到到D盘? 安装 ...
- 学习笔记:安装swig+用SWIG封装C++为Python模块+SWIG使用说明
这段时间一直在摸索swing,用它来封装C++代码来生成python脚步语言.并总结了swing从安装到配置再到代码封装编译生成动态库的整个过程,下面这篇文章都是我在实际的运用中的一些经验总结,分享给 ...
- 一 :了解MVC
介绍 1. ASP.NET WebForm和ASP.NET MVC是并行的关系.都是属于.NET框架下的子框架. 2. MVC项目常用模板 空模板 : 不包含MVC目录结构,需要自己添加. 基本模 ...
- golang docker kubernetes
不断共建Golang生态.其中比较有代表性的Golang编写软件作品是Docker和Kubernetes.从目前Golang的发展时间和社区活跃度来看,Golang无疑是一门成功的编程语言.
- Cobbler安装和配置
1.yum国内源的安装与更新 1.1 备份原repo文件 cd /etc/yum.repo.d/ mkdir repo_bak mv *.repo repo_bak 1.2 在centos中配置网易和 ...
- 如何创建vue项目
Vue项目环境搭建 """ node ~~ python:node是用c++编写用来运行js代码的 npm(cnpm) ~~ pip:npm是一个终端应用商城,可以换国内 ...
- quartz (从原理到应用)详解篇(转)
一.Quartz 基本介绍 1.1 Quartz 概述 1.2 Quartz特点 1.3 Quartz 集群配置 二.Quartz 原理及流程 2.1 quartz基本原理 2.2 quartz启动流 ...
- Codeforces Round #455 (Div. 2) D题(花了一个早自习补了昨晚的一道模拟QAQ)
D. Colorful Points You are given a set of points on a straight line. Each point has a color assigned ...
- 快速弄清JavaScript中undefined和null的区别
ES6的7大数据类型里面有这俩玩意:undefined和null,让接触不深的学习者常常产生混淆,这俩玩意的区别在哪呢? 字面意思上来看,undefined是未(被我们)阐明的,未说明的,null则意 ...
- 掌握 3 个搜索技巧,在 GitHub 上快速找到实用软件资源
GitHub 作为目前广大程序猿最大的游乐场,在今年 6 月被 微软 以 75 亿美元价值的微软股票收购,GitHub 再次成为业界讨论的焦点.GitHub 以自由开放的定位吸引了相当多的个人开发者和 ...