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的更多相关文章

  1. MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult

    导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...

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

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

  3. mvc导出excel 之 新

    前段时间做的mvc导出excel 老大说要进行优化,我原来导出是用npoi插件进行导出,格式是将数据放入到datatable中,然后进行导出. 说要优化的时候就想着将datatable数据导出格式改为 ...

  4. (转)原始图像数据和PDF中的图像数据

    比较原始图像数据和PDF中的图像数据,结果见表1.1.表1.1中各种“解码器”的解释见本文后续的“PDF支持的图像格式”部分,“PDF中的图像数据”各栏中的数据来自开源的PdfView.如果您有兴趣查 ...

  5. 导出excel和PDF小结 vba

    最近接触了一个关于Access工具的项目,所以整理下需要使用的方法. 功能要求简介: 1.将数据表中的数据导出到excel和PDF 2.并根据某个字段名称分sheet输出. 3.无模板方式 方案简介: ...

  6. PLSQL_数据泵导入导出数据Impdp/ Expdp(概念)

    2014-08-31 Created By BaoXinjian

  7. ASP.NET MVC导出excel

    ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...

  8. ASP.NET MVC 导出Word报表

    最近要做MVC导出Word报表功能.查了查资料发现一个好用的插件就是Aspose.Word.这个插件也很有名气,也很好用. 1.首先就是引用该插件 2.填充Word模版 3.后台操作 private ...

  9. 导出数据到Excel表格

    开发工具与关键技术:Visual Studio 和 ASP.NET.MVC,作者:陈鸿鹏撰写时间:2019年5月25日123下面是我们来学习的导出数据到Excel表格的总结首先在视图层写导出数据的点击 ...

随机推荐

  1. ES7.x mapping 类型

    在将ES从2.3 升级到7.3版本的过程中,mapping是一个过不去的坎,很多类型都发生了变化 7.x常用数据类型:text.keyword.number.array.range.boolean.d ...

  2. RabbitMQ 功能

    学习完了rabbitmq总一下 RabbitMQ依赖的语言 erlang 第一它可以实现不同程序之间的程序信息储存交互,在易用性.扩展性.高可用性的方面不俗. rabbitmq相当于一个中间人,我们同 ...

  3. SSH环境搭建之Spring环境搭建篇

    SSH环境搭建之Spring环境搭建篇 一.引入Spring所使用的JAR文件 二.在src目录下创建beans.xml(Spring的容器文件) <?xml version="1.0 ...

  4. Event Binding in Angular

    https://www.pluralsight.com/guides/angular-event-binding Introduction In this guide, we will explore ...

  5. dockerfile-maven plugin自动镜像制作并发布

    环境准备:win10+docker 1.打开hyper-v 2.下载最新版本docker:https://store.docker.com/editions/community/docker-ce-d ...

  6. log4j.xml常用配置

    Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...

  7. Vue公共结果页面实现

    需求 我希望写一个公共结果页面,满足所有模块的结果展示,页面设计要素如下: 结果图标type(成功图标,失败图标) 标题title(如:提交成功) 描述descripton(如:您的工单已提交,请等待 ...

  8. CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26

    /* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...

  9. vue cli 3.x 配置使用 sourceMap

    项目使用vue cli 3.x搭建,没有了配置文件,如何更方便的查找到对应的scss文件,配置项目支持sourceMap方式? 分二步走: 1.项目根目录(不是src目录,不要搞错了)添加vue.co ...

  10. vue中前进刷新、后退缓存用户浏览数据和浏览位置的实践

    vue中前进刷新.后退缓存用户浏览数据和浏览位置的实践 2018年07月07日 11:58:40 大灰狼的小绵羊哥哥 阅读数:4492   vue中,我们所要实现的一个场景就是: 1.搜索页面==&g ...