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表格的总结首先在视图层写导出数据的点击 ...
随机推荐
- 第一章 Django之MVC模式(2)
让我们来研究一个简单的例子,通过该实例,你可以分辨出,通过 Web 框架来实现的功能与之前的方式有何不同.下面就是通过使用 Django 来完成以上功能的例子: # models.py (the da ...
- Image Processing and Computer Vision_Review:A survey of recent advances in visual feature detection(Author's Accepted Manuscript)——2014.08
翻译 一项关于视觉特征检测的最新进展概述(作者已被接受的手稿) 和A survey of recent advances in visual feature detection——2014.08内容相 ...
- Redis分布式缓存安装和使用
独立缓存服务器: LinuxCentOS Redis版本: 3.0 下面我们针对于Redis安装做下详细的记录: 编译和安装所需的包: #yum install gcc tcl创建安装目录:贵州中医肝 ...
- 【Linux常用命令】Linux kill, killall, kill -9,
参考链接 https://blog.csdn.net/zong596568821xp/article/details/77899454 kill + PID kill -9 + PID 加上-9 是 ...
- 牛客练习赛42 C 出题的诀窍 (贡献,卡常)
牛客练习赛42 C 出题的诀窍 链接:https://ac.nowcoder.com/acm/contest/393/C来源:牛客网 题目描述 给定m个长为n的序列a1,a2,-,ama_1 , a_ ...
- Event Binding in Angular
https://www.pluralsight.com/guides/angular-event-binding Introduction In this guide, we will explore ...
- Atcoder Educational DP Contest 题解
A - Frog 1/B - Frog 2 入门... #include<cstdio> #define abs(a) ((a)>=0?(a):(-(a))) #define min ...
- appium连接Android真机,并调试
Android真机:华为pad2 Android和Windows连接同一个局域网 连接USB Android机设置: 打开USB调试 cmd>adb devices,显示Android序列号 , ...
- JS用正则替换特殊字符
'abcabce'.replace(/abc/g,'') "e" 'abcabce$E$'.replace(/$E$/g,'') "abcabce$E$" 'a ...
- pandas中DataFrame和Series的数据去重
在SQL语言中去重是一件相当简单的事情,面对一个表(也可以称之为DataFrame)我们对数据进行去重只需要GROUP BY 就好. select custId,applyNo from tmp.on ...