/// <summary> /// 获得多页的PDF文件 /// </summary> /// <param name="pdf1">模板文件</param> /// <param name="llfi">按页内容LIST文件</param> /// <param name="pdfout">输出文件</param> /// <returns>页数</returns> public static int GetNPagesBuf(string pdf1, List <List <FieldInfo> > llfi, string pdfout) { //一下仅用于进度条start CProcessInfo info = HttpContext.Current.Session["ProcessInfo"] as CProcessInfo; int iret = 0; byte[] bout = null; //一下仅用于进度条start info.Message = "PDF数据流加载"; iret = GetNPagesBuf(pdf1, llfi, out bout); //一下仅用于进度条start info.Message = "PDF数据流写入开始"; File.WriteAllBytes(pdfout, bout); //一下仅用于进度条start info.Message = "PDF数据流写入完成"; return(iret); }
/// <summary> /// 根据填充内容获得一页的PDF文件内容 /// </summary> /// <param name="pdf1">模板文件</param> /// <param name="lfi">数据LIST</param> /// <param name="buf">返回的BUFFER</param> /// <returns>0为不成功</returns> public static int Get1PageBuf(string pdf1, List <FieldInfo> lfi, out byte[] buf) { int iret = 0; MemoryStream ms = new MemoryStream(); PdfReader reader = new PdfReader(pdf1); PdfStamper stamp1 = new PdfStamper(reader, ms); //不关闭流 stamp1.Writer.CloseStream = false; AcroFields form1 = stamp1.AcroFields; //BaseFont.AddToResourceSearch("iTextAsian.dll"); //BaseFont.AddToResourceSearch("iTextAsianCmaps.dll"); BaseFont.AddToResourceSearch(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\bin\\iTextAsian.dll"); BaseFont.AddToResourceSearch(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\bin\\iTextAsianCmaps.dll"); BaseFont font = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); //以下为填写表单部分 for (int i = 0; i < lfi.Count; i++) { form1.SetFieldProperty(lfi[i].FieldName, "textfont", font, null); form1.SetField(lfi[i].FieldName, lfi[i].FieldValue); } //一下仅用于进度条start CProcessInfo info = HttpContext.Current.Session["ProcessInfo"] as CProcessInfo; info.Message = lfi[0].FieldValue + "创建打印文件完成"; info.Current++; //一下仅用于进度条end stamp1.FormFlattening = true; stamp1.Close(); buf = ms.GetBuffer(); iret = 1; return(iret); }
/// <summary> /// 得到多页的PDF文件 /// </summary> /// <param name="pdf1">模板</param> /// <param name="llfi"></param> /// <param name="buf"></param> /// <returns></returns> public static int GetNPagesBuf(string pdf1, List <List <FieldInfo> > llfi, out byte[] buf) { //一下仅用于进度条start CProcessInfo info = HttpContext.Current.Session["ProcessInfo"] as CProcessInfo; info.Message = "PDF文件生成开始"; info.TotalCount = llfi.Count; //一下仅用于进度条start int iret = 0; List <byte[]> lbs = new List <byte[]>(); for (int i = 0; i < llfi.Count; i++) { byte[] b = null; Get1PageBuf(pdf1, llfi[i], out b); lbs.Add(b); } iret = MergePDFPages(lbs, out buf); return(iret); }
/// <summary> /// 根据模板生成Excel表(不带表头); /// </summary> /// <param name="dt">数据源</param> /// <param name="strExcelName">输出excel</param> /// <param name="strTemplateName">模板名称</param> /// <param name="Title">表格标题</param> public static void CreateByTemplateWithoutTitle(System.Data.DataTable dt, string strExcelName, string strTemplateName) { Excel.Application objExcel = new Excel.Application(); //EXCEL实例 Workbooks objBooks; //工作簿 集合 Workbook objBook; //工作簿 Sheets objSheets; //工作表集合 Worksheet objSheet; //工作表 Range objCells; //单元格 string strFile = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + ExcelPath + strExcelName; //下载路径 string strTemplate = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + TempletPath + strTemplateName; //模板路径 objExcel.Visible = false; objExcel.DisplayAlerts = false; //定义一个新的工作簿 objBooks = objExcel.Workbooks; objBooks.Open(strTemplate, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); objBook = objBooks.get_Item(1); objSheets = objBook.Worksheets; objSheet = (Worksheet)objSheets.get_Item(1); //命名该sheet objSheet.Name = "导出数据"; objCells = objSheet.Cells; //将数据导入到Excel中去 int intRow; //行号 int intCol; //列号 int RowNumber = 0; int ColNumber = 0; ColNumber = dt.Columns.Count; RowNumber = dt.Rows.Count + 1; CProcessInfo info = HttpContext.Current.Session["ProcessInfo"] as CProcessInfo; //info.Message = "excel标题生成"; ////设置表名字体 //Excel.Range TitleRange = objSheet.Range[objSheet.Cells[1, 1], objSheet.Cells[1, ColNumber]]; //TitleRange.Font.Name = "宋体"; //TitleRange.Font.Size = 16; //TitleRange.Font.Bold = true; //TitleRange.HorizontalAlignment = Excel.Constants.xlCenter; ////拼合单元格 //TitleRange.Merge(Type.Missing); ////设置表名 //objCells[1, 1] = Title; //添加列标题 for (intCol = 0; intCol < dt.Columns.Count; intCol++) { objCells[1, intCol + 1] = dt.Columns[intCol].ToString(); } //设置列标题字体 Excel.Range ColumTitleRange = objSheet.Range[objSheet.Cells[1, 1], objSheet.Cells[1, ColNumber]]; ColumTitleRange.Font.Name = "宋体"; ColumTitleRange.Font.Size = 12; ColumTitleRange.Font.Bold = true; ColumTitleRange.HorizontalAlignment = Excel.Constants.xlCenter; ////设定起始单元格位置 Excel.Range myrange = objSheet.Range[objSheet.Cells[1, 1], objSheet.Cells[RowNumber, ColNumber]]; //文本格式 myrange.NumberFormatLocal = "@ "; //添加内容 for (intRow = 0; intRow < dt.Rows.Count; intRow++) { DataRow dr = dt.NewRow(); dr = dt.Rows[intRow]; for (intCol = 0; intCol < dt.Columns.Count; intCol++) { objCells[intRow + 2, intCol + 1] = dr[intCol].ToString(); } info.Message = "第" + intRow.ToString() + "数据插入"; info.Current = info.Current + intRow; } //单元格自适应宽度 myrange.Columns.AutoFit(); myrange.Rows.AutoFit(); //加入表格边框 Excel.Borders pborders = myrange.Borders; //设置左边框 pborders.get_Item(XlBordersIndex.xlEdgeLeft); pborders.LineStyle = Excel.XlLineStyle.xlContinuous; pborders.Weight = Excel.XlBorderWeight.xlThin; pborders.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; object pObject = myrange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null); ////设置上边框线 pborders.get_Item(XlBordersIndex.xlEdgeTop); pborders.LineStyle = Excel.XlLineStyle.xlContinuous; pborders.Weight = Excel.XlBorderWeight.xlThin; pborders.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; pObject = myrange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null); ////设置下边框线 pborders.get_Item(XlBordersIndex.xlEdgeBottom); pborders.LineStyle = Excel.XlLineStyle.xlContinuous; pborders.Weight = Excel.XlBorderWeight.xlThin; pborders.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; pObject = myrange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null); ////设置右边框线 pborders.get_Item(XlBordersIndex.xlEdgeRight); pborders.LineStyle = Excel.XlLineStyle.xlContinuous; pborders.Weight = Excel.XlBorderWeight.xlThin; pborders.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; pObject = myrange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null); ////设置内格竖线 pborders.get_Item(XlBordersIndex.xlInsideVertical); pborders.LineStyle = Excel.XlLineStyle.xlContinuous; pborders.Weight = Excel.XlBorderWeight.xlThin; pborders.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; pObject = myrange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null); ////设置内格横边框线 pborders.get_Item(XlBordersIndex.xlInsideHorizontal); pborders.LineStyle = Excel.XlLineStyle.xlContinuous; pborders.Weight = Excel.XlBorderWeight.xlThin; pborders.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic; pObject = myrange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, null); //保存到临时文件夹 objSheet.SaveAs(strFile, Excel.XlFileFormat.xlTemplate, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); objBook.Close(false, Type.Missing, Type.Missing); //退出Excel,并且释放调用的COM资源 objExcel.Quit(); //回收资源 GC.Collect(); //关闭进程 //KillProcess("Excel"); //以字符流的形式下载文件 FileStream fs = new FileStream(strFile, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); // CFile.FileDown(strFile, "1"); //Response.ContentType = "application/octet-stream"; ////通知浏览器下载文件而不是打开 //string strValue = "attachment; filename=" + HttpUtility.UrlEncode(strFile, System.Text.Encoding.UTF8) ; //Response.AddHeader("Content-Disposition", strValue); //Response.BinaryWrite(bytes); //Response.Flush(); ////删除临时EXCEL文件 // File.Delete(strFile); //Response.End(); }
/// <summary> /// 根据模板生成Excel表 /// </summary> /// <param name="dt">数据源</param> /// <param name="strExcelName">输出excel</param> /// <param name="strTemplateName">模板名称</param> /// <param name="Title">表格标题</param> public static void CreateWithTitle(System.Data.DataTable dt, string strExcelName, string strTemplateName) { Excel.Application objExcel = new Excel.Application(); //EXCEL实例 Workbooks objBooks; //工作簿 集合 Workbook objBook; //工作簿 Sheets objSheets; //工作表集合 Worksheet objSheet; //工作表 Range objCells; //单元格 string strFile = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + ExcelPath + strExcelName; //下载路径 string strTemplate = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + TempletPath + strTemplateName; //模板路径 objExcel.Visible = false; objExcel.DisplayAlerts = false; //定义一个新的工作簿 objBooks = objExcel.Workbooks; objBooks.Open(strTemplate, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); objBook = objBooks.get_Item(1); objSheets = objBook.Worksheets; objSheet = (Worksheet)objSheets.get_Item(1); //命名该sheet objSheet.Name = "导出数据"; objCells = objSheet.Cells; //将数据导入到Excel中去 int intRow; //行号 int intCol; //列号 int RowNumber = 0; int ColNumber = 0; ColNumber = dt.Columns.Count; RowNumber = dt.Rows.Count + 1; CProcessInfo info = HttpContext.Current.Session["ProcessInfo"] as CProcessInfo; info.Message = "excel标题生成"; //文本格式 Excel.Range myrange = objSheet.Range[objSheet.Cells[1, 1], objSheet.Cells[RowNumber, ColNumber]]; myrange.NumberFormatLocal = "@ "; //添加标题 for (intCol = 0; intCol < dt.Columns.Count; intCol++) { objCells[1, intCol + 1] = dt.Columns[intCol].ToString(); } //添加内容 for (intRow = 0; intRow < dt.Rows.Count; intRow++) { DataRow dr = dt.NewRow(); dr = dt.Rows[intRow]; for (intCol = 0; intCol < dt.Columns.Count; intCol++) { objCells[intRow + 2, intCol + 1] = dr[intCol].ToString(); } info.Message = "第" + intRow.ToString() + "数据插入"; info.Current = info.Current + intRow; } //保存到临时文件夹 objSheet.SaveAs(strFile, Excel.XlFileFormat.xlTemplate, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); objBook.Close(false, Type.Missing, Type.Missing); //退出Excel,并且释放调用的COM资源 objExcel.Quit(); //回收资源 GC.Collect(); //关闭进程 //KillProcess("Excel"); //以字符流的形式下载文件 FileStream fs = new FileStream(strFile, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); // CFile.FileDown(strFile, "1"); //Response.ContentType = "application/octet-stream"; ////通知浏览器下载文件而不是打开 //string strValue = "attachment; filename=" + HttpUtility.UrlEncode(strFile, System.Text.Encoding.UTF8) ; //Response.AddHeader("Content-Disposition", strValue); //Response.BinaryWrite(bytes); //Response.Flush(); ////删除临时EXCEL文件 // File.Delete(strFile); //Response.End(); }