java中解析json格式字符串---生成pdf报表文件

被java如何生成pdf报表格式文件给折磨了好几天,总算告一段落~~~今天晚上得空分享出来供小伙伴们借鉴!!

一.先看通过itext生成的pdf文档效果

二.源代码

package com.sixosoft.oa.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ItextTest {
	private static Font headfont;// 设置字体大小
	private static Font keyfont;// 设置字体大小
	private static Font textfont;// 设置字体大小

	private static int maxWidth = 520;
	static {
		BaseFont bfChinese;
		try {
			// bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置字体用宋体
			bfChinese = BaseFont.createFont("c:\\windows\\fonts\\STZHONGS.TTF", BaseFont.IDENTITY_H,
					BaseFont.NOT_EMBEDDED);// 华文中宋
			headfont = new Font(bfChinese, 18, Font.BOLD);// 标题字体大小
			keyfont = new Font(bfChinese, 8, Font.BOLD);// 关键标题字体大小
			textfont = new Font(bfChinese, 8, Font.NORMAL);// 字段字体大小
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * 创建带部分属性的表格
	 * 
	 * @param colNumber 列数
	 * @return
	 */
	public static PdfPTable createTable(int colNumber) {
		PdfPTable table = new PdfPTable(colNumber);
		try {
			table.setTotalWidth(maxWidth);
			table.setLockedWidth(true);
			table.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.getDefaultCell().setBorder(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return table;
	}
	/**
	 * 一般用于创建表格标题
	 * @param value  单元格内容
	 * @param font	字体大小
	 * @param align 垂直方向对齐方式 (ALIGN_CENTER, ALIGN_LEFT, ALIGN_MIDDLE,ALIGN_RIGHT)
	 * @param colspan 所占列宽
	 * @param boderFlag 是否设置padding
	 * @return
	 */
	public static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(align);
		cell.setColspan(colspan);
		cell.setPhrase(new Phrase(value, font));
		cell.setPadding(3.0f);
		if (!boderFlag) {
			cell.setBorder(0);
			cell.setPaddingTop(15.0f);
			cell.setPaddingBottom(8.0f);
		}
		return cell;
	}
	/**
	 * 一般用于创建表格标题行
	 * @param value 单元格内容
	 * @param font  字体大小
	 * @param align 垂直方向对齐方式 (ALIGN_CENTER, ALIGN_LEFT, ALIGN_MIDDLE,ALIGN_RIGHT)
	 * @return
	 */
	public static PdfPCell createCell(String value,com.itextpdf.text.Font font,int align){
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value,font));
        return cell;
    }
	/**
	 * 一般用于创建表格正文内容
	 * @param value 单元格内容
	 * @param font  字体大小
	 * @return
	 */
	public static PdfPCell createCell(String value,Font font){
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value,font));
        cell.setPadding(6.0f);
        return cell;
    }
	
	/**
	 * 生成派工单pdf文件,传入参数为json字符串
	 * 
	 * @throws IOException
	 * @throws DocumentException
	 */
	public void CreatePGDpdf(String json) {
		// 在指定目录下创建一个文件
		File file = new File("C:\\Users\\Administrator\\Desktop\\test.pdf");
		try {
			file.createNewFile();

			// 建立一个Document对象
			Document document = new Document();
			// 设置页面大小
			document.setPageSize(PageSize.A4);
			// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
			PdfWriter.getInstance(document, new FileOutputStream(file));
			document.open();
			
			//从参数中取出主表数据
			JSONObject jsonObject = JSONObject.fromObject(json);
			JSONObject paigong = jsonObject.getJSONObject("paigong");
			String k_recordsnum = paigong.getString("RECORDS_NUM");
			String k_bxadress = paigong.getString("BXADRESS");
			String k_bxr = paigong.getString("BXR");
			String k_lxdh = paigong.getString("LXDH");
			String k_bxcontext = paigong.getString("BXCONTEXT");
			String k_bxtime = paigong.getString("BXTIME");
			String k_jbr = paigong.getString("JBR");
			String k_wxxm = paigong.getString("WXXM");
			String k_yysj = paigong.getString("YYSJ");
			String k_dctime = paigong.getString("DCTIME");
			String k_wgtime = paigong.getString("WGTIME");
			String k_wxjg = paigong.getString("WXJG");
			String k_wxr = paigong.getString("WXR");
			String k_rgfy = paigong.getString("RGFY");
			String k_zfy = paigong.getString("ZFY");
			String k_wxpj = paigong.getString("WXPJ");
			
			// 向文档中添加内容
			Paragraph paragraph = new Paragraph("客户维修派工单",headfont);
			paragraph.setAlignment(1);
			document.add(paragraph);
			document.add(new Paragraph("\n"));
			
			//   \t不起作用,没办法只能用n多的空格代替
			String table_up = "编号 : "+""+"                                                                       " + 
							  "日期 : "+k_bxtime+"                             "+
							  "单号 : "+k_recordsnum;
			paragraph = new Paragraph(table_up,textfont);
			paragraph.setAlignment(1);
			document.add(paragraph);
			document.add(new Paragraph("\n"));			

			PdfPTable table = createTable(6);
			//第一行内容
			//table.addCell(createCell("客户维修派工单", headfont, Element.ALIGN_CENTER, 6, true));
			table.addCell(createCell("房号", textfont));
			table.addCell(createCell(k_bxadress, textfont));
			table.addCell(createCell("客户名称", textfont));
			table.addCell(createCell(k_bxr, textfont));
			table.addCell(createCell("联系电话", textfont));
			table.addCell(createCell(k_lxdh, textfont));
			
			//第二行内容
			PdfPCell cell = createCell("报修内容", textfont);		
			cell.setRowspan(2);				
			table.addCell(cell);			
			cell = createCell(k_bxcontext, textfont);			
			cell.setRowspan(2);
			cell.setColspan(3);
			table.addCell(cell);			
			table.addCell(createCell("报修时间", textfont));
			cell = createCell(k_bxtime, textfont);
			cell.setNoWrap(true);
			table.addCell(cell);
			table.addCell(createCell("记录人", textfont));
			table.addCell(createCell(k_jbr, textfont));
			
			//第三行内容
			cell = createCell("维修内容", textfont);		
			cell.setRowspan(3);				
			table.addCell(cell);			
			cell = createCell(k_wxxm, textfont);			
			cell.setRowspan(3);
			cell.setColspan(3);
			table.addCell(cell);			
			table.addCell(createCell("预约维修时间", textfont));
			cell = createCell(k_yysj, textfont);
			cell.setNoWrap(true);   //不自动换行
			table.addCell(cell);
			table.addCell(createCell("开工时间", textfont));
			cell = createCell(k_dctime, textfont);
			cell.setNoWrap(true);
			table.addCell(cell);
			table.addCell(createCell("完工时间", textfont));
			cell = createCell(k_wgtime, textfont);
			cell.setNoWrap(true);
			table.addCell(cell);
//			table.addCell(createCell("开工时间", textfont));
//			table.addCell(createCell(k_dctime, textfont));
//			table.addCell(createCell("完工时间", textfont));
//			table.addCell(createCell(k_wgtime, textfont));
			
			//第四行内容
			table.addCell(createCell("未完原因及处理", textfont));
			cell = createCell(k_wxjg, textfont);		
			cell.setColspan(3);				
			table.addCell(cell);
			table.addCell(createCell("维修人员", textfont));
			table.addCell(createCell(k_wxr, textfont));
			
			//第五行内容
			table.addCell(createCell("使用材料名称", textfont));
			table.addCell(createCell("材料提供方", textfont));
			table.addCell(createCell("规格", textfont));
			table.addCell(createCell("数量", textfont));
			table.addCell(createCell("单价(元)", textfont));
			table.addCell(createCell("材料总价(元)", textfont));
			
			//维修明细表数据行
			//从参数中取出明细表数据
			JSONArray wxclmx = paigong.getJSONArray("wxclmx");
			JSONObject row = null;
			for (int i = 0; i < wxclmx.size(); i++) {
				//每一条明细记录
				row = wxclmx.getJSONObject(i);
				String k_wlname = row.getString("WL_NAME");
				String k_ckname = row.getString("CK_NAME");
				String k_jldw = row.getString("JLDW");
				String k_sl = row.getString("SL");
				String k_dj = row.getString("DJ");
				String k_je = row.getString("JE");				
				//插入每条明细数据
				table.addCell(createCell(k_wlname, textfont));
				table.addCell(createCell(k_ckname, textfont));
				table.addCell(createCell(k_jldw, textfont));
				table.addCell(createCell(k_sl, textfont));
				table.addCell(createCell(k_dj, textfont));
				table.addCell(createCell(k_je, textfont));				
			}			
			
			//第六行内容
			table.addCell(createCell("人工费(元)", textfont));
			table.addCell(createCell(k_rgfy, textfont));
			table.addCell(createCell("费用合计(元)", textfont));
			table.addCell(createCell(k_zfy, textfont));
			table.addCell(createCell("收费确认", textfont));
			table.addCell(createCell("", textfont));
						
			//第七行内容
			table.addCell(createCell("客户认可签名", textfont));
			cell = createCell(k_wxpj, textfont);		
			cell.setColspan(3);				
			table.addCell(cell);
			table.addCell(createCell("工程负责人确认", textfont));
			table.addCell(createCell("", textfont));
			
			document.add(table);
			
			//备注
			paragraph = new Paragraph("备注 : 第一联  客服部存档 ; 第二联  客户留存 ; 第三联  工程部存档", textfont);
			document.add(paragraph);
	        	        
			// 关闭文档
			document.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		String json = "{\"paigong\":{\"RECORDS_NUM\":\"201801070001\",\"BXTIME\":\"2018-01-07 10:25:32\",\"BXADRESS\":\"国务院A栋 1层 A102\",\"LOCATION\":\"\",\"YYSJ\":\"\",\"BXR\":\"\",\"LXR\":\"vicky\",\"LXDH\":\"18888888888\",\"BXLB\":\"空调\",\"WXXM\":\"\",\"JBR\":\"张三\",\"WBDW\":\"\",\"BXCONTEXT\":\"空调维修\",\"PGTIME\":\"2018-01-07 11:06:41\",\"PGR\":\"李四\",\"WXR\":\"李军\",\"DCTIME\":\"2018-01-07 09:30:00\",\"WGTIME\":\"2018-01-07 10:30:00\",\"FWLX\":\"收费\",\"WXJG\":\"完成\",\"WXPJ\":\"满意\",\"WXCONTENT\":\"\",\"CLFY\":\"22\",\"RGFY\":\"28\",\"ZFY\":\"50\",\"KF_NAME\":\"一园项目物料库房\",\"wxclmx\":[{\"CK_NAME\":\"一园项目物料库房\",\"WL_NAME\":\"A4复印纸\",\"SL\":\"2\",\"DJ\":\"11\",\"JE\":\"22\",\"JLDW\":\"包\"}]}}";
				
		ItextTest printTest = new ItextTest();
		printTest.CreatePGDpdf(json);
	}

}

三.一般情况会报错,因为还没有导入相应的包

      1.如果创建的pdf文档传入的参数不是json格式的字符串,只需要导入itext5的两个jar包即可;

          

           第一个是向pdf中写入中文时,需要的jar包;          第二个是itex5的jar包

           https://download.csdn.net/download/miracle_gaaral/10902367

        2.当从前端传入json字符串格式的内容时,就需要导入解析json字符串的jar包了(有6个,都不能少哦~~);

           

           https://download.csdn.net/download/miracle_gaaral/10902371

       导入以上包后,运行程序,在桌面就会出现test.pdf文档了~

          

四.JFinal中通过POST传json字符串时,遇到getPara()方法怎么也接收不到的情况,但通过GET方式或在url链接后加参数却没问题(不是这个开发框架的或没有这种情况的可以直接忽略)--------------可以试试这个方式获取打印看看

        String jsonStr = HttpKit.readData(getRequest());        

参考文档:如有侵权~请联系删除:

1.https://www.cnblogs.com/shuilangyizu/p/5760928.html

2.https://blog.csdn.net/lingmao555/article/details/78320689

3.http://www.jfinal.com/share/231

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java Fastjson是一个高性能的JSON解析库,它提供了简洁而灵活的API,方便我们解析JSON数组字符串。 使用Fastjson解析JSON数组字符串的方法如下: 首先,我们需要导入Fastjson库,可以从官方网站下载Fastjson的JAR文件,然后将其添加到项目的依赖。 接下来,我们可以使用Fastjson提供的API来解析JSON数组字符串。我们可以通过`JSONArray.parseArray()`方法将JSON数组字符串解析为一个Java List对象,代码示例如下: ```java import com.alibaba.fastjson.JSONArray; public class JsonArrayParser { public static void main(String[] args) { String jsonArrayStr = "[{\"name\":\"张三\",\"age\":18},{\"name\":\"李四\",\"age\":20}]"; // 解析JSON数组字符串 JSONArray jsonArray = JSONArray.parseArray(jsonArrayStr); // 遍历数组元素 for (Object obj : jsonArray) { // 获取每个JSON对象的属性值 String name = ((JSONObject) obj).getString("name"); int age = ((JSONObject) obj).getIntValue("age"); // 打印结果 System.out.println("姓名:" + name + ",年龄:" + age); } } } ``` 在上述代码,我们首先定义了一个JSON数组字符串`jsonArrayStr`,然后使用`JSONArray.parseArray()`方法将其解析为一个JSONArray对象。 接下来,我们可以使用`for`循环遍历JSONArray的每个元素。通过强制类型转换将每个元素转换为JSONObject,然后通过`.getString()`和`.getIntValue()`方法获取每个JSON对象的属性值。 最后,我们可以对获取的属性值进行适当的处理,如打印输出,存入数据库等。 总结来说,使用Fastjson解析JSON数组字符串主要是借助`JSONArray.parseArray()`方法将JSON数组字符串解析JSONArray对象,然后通过遍历JSONArray获取每个JSON对象的属性值进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值