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表格的总结首先在视图层写导出数据的点击 ...
随机推荐
- ES bulk 批量操作
bulk允许在一个请求中进行多个操作(create.index.update.delete),也就是可以在一次请求裡做很多事情 也由于这个关系,因此bulk的请求体和其他请求的格式会有点不同 bulk ...
- 【转】char data[0]用法总结
@2019-07-31 struct MyData { int nLen; ]; }; 开始没有理解红色部分的内容,上网搜索下,发现用处很大,记录下来. 在结构中,data是一个数组名:但该数组没有元 ...
- Ubuntu系统---安装 WPS
Ubuntu系统---安装 WPS Ubuntu桌面系统自带了Libreoffice办公软件,但是个人觉得它不符合我们中国人的使用习惯.搜索了Office For Linux,好麻烦,也会出现问题, ...
- Physical Education Lessons CodeForces - 915E (动态开点线段树)
Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...
- kotlin字符串模板&条件控制
字符串模版: 小时候都有要求记日记的习惯,下面是一小学生记的日记: 很漂亮的流水账,那细分析一下这些文件其实大体都类似,只有几个不同点: 其实就是地点变了,那对于这种有规律的文字可以采用kotlin的 ...
- mysql大数据解决方案--分表分库(0)
引言 对于一个大型的互联网应用,海量数据的存储和访问成为了系统设计的瓶颈问题,对于系统的稳定性和扩展性造成了极大的问题.通过数据切分来提高网站性能,横向扩展数据层已经成为架构研发人员首选的方式. •水 ...
- ubuntu卸载/更新Cmake
CMake安装或CMake Error at CMakeLists 发生情景: 使用cmake命令安装软件时,报如下错误: CMake Error at CMakeLists.txt:4 (CMAKE ...
- 【Android-数据库Sqlite】Sqlite数据库 增、删、改、查
1.先创建一个Product类 Product.java 变量如下: int id; String code; String name; int qty; 2.创建一个DBHelper类 DBHelp ...
- springboot 集成swagger2.x 后静态资源报404
package com.bgs360.configuration; import org.springframework.context.EnvironmentAware; import org.sp ...
- Java进阶知识08 Hibernate多对一单向关联(Annotation+XML实现)
1.Annotation 注解版 1.1.在多的一方加外键 1.2.创建Customer类和Order类 package com.shore.model; import javax.persisten ...