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. 404boom 博客闪现【不断的优化更新中。。。】

    404boom 博客闪现[不断的优化更新中...] 停止本篇博文EQ继续优化,所有博文将会在标签[cnblogs_v2 ]中重新整理,待完成统一放上链接 一:Java SE相关1.Java的概述2.J ...

  2. 01_Redis简述

    一:关系型数据库和非关系型数据库的区别: 1:关系型数据库(SQL):数据和数据之间,表和字段之间,表和表之间是存在关系的: 优点:数据之间有关系,进行数据的增删改查时非常方便的:关系型数据库有事务操 ...

  3. js获取div基础元素

    1.js获取div元素 clientHeight 获取对象的高度,不计算任何边距.边框.滚动条,但包括该对象的补白. clientLeft 获取 offsetLeft 属性和客户区域的实际左边之间的距 ...

  4. BZOJ2278 [Poi2011]Garbage[欧拉回路求环]

    首先研究环上性质,发现如果状态不变的边就不需要动了,每次改的环上边肯定都是起末状态不同的边且仅改一次,因为如果有一条边在多个环上,相当于没有改,无视这条边之后,这几个环显然可以并成一个大环.所以,我们 ...

  5. Java笔记(第六篇-网络通信)

    TCP/IP模式是一种层次结构,共分为四层,分别为应用层.传输层.互联网层和主机到网络层. 在TCP/IP协议栈中,有两个高级协议是网络应用程序编写者应该了解的,即“传输控制协议”(TCP)与“用户数 ...

  6. lnmp配置

    yum源切换 下载wegt工具 yum install -y wget 备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS- ...

  7. Unity 截图选择框,中间全透明,边缘半透明

    效果:点击白色框可拖拽选择区域 代码: using System.Collections; using System.Collections.Generic; using UnityEngine; u ...

  8. HDU 6085 - Rikka with Candies | 2017 Multi-University Training Contest 5

    看了标程的压位,才知道压位也能很容易写- - /* HDU 6085 - Rikka with Candies [ 压位 ] | 2017 Multi-University Training Cont ...

  9. Laravel 多态关联中不能使用 has, whereHas

    ghost commented on Apr 13, 2017 • edited by ghost  i'm currently using this code in my own project m ...

  10. 51 Nod 1509 加长棒(巧妙的隔板法计数)

    1509 加长棒  题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 现在有三根木棒,他们的长度分别是a,b,c厘米 ...