生成条形码,java操作word文档

/*
	 * 生成条形码
	 */
	@SuppressWarnings({ "restriction", "unused" })
	static String getImageBinary(String user_id, String temp) {
		try {
			JBarcode localJBarcode = new JBarcode(Code39Encoder.getInstance(),
					WidthCodedPainter.getInstance(),
					EAN13TextPainter.getInstance());
			// 生成. 欧洲商品条码(=European Article Number)
			// 这里我们用作图书条码
			String str = user_id;
			BufferedImage localBufferedImage = localJBarcode.createBarcode(str);
			saveToPNG(temp, localBufferedImage, "EAN13.png");
			localJBarcode.setEncoder(CodabarEncoder.getInstance());
			localJBarcode.setPainter(WideRatioCodedPainter.getInstance());
			localJBarcode.setTextPainter(BaseLineTextPainter.getInstance());
			localJBarcode.setShowCheckDigit(false);
			// xx
			str = user_id;
			localBufferedImage = localJBarcode.createBarcode(str);
			saveToPNG(temp, localBufferedImage, "Code39.png");

		} catch (Exception localException) {
			localException.printStackTrace();
		}
		BASE64Encoder encoder = new sun.misc.BASE64Encoder();
		BASE64Decoder decoder = new sun.misc.BASE64Decoder();
		File f = new File(temp + "barcode//Code39.png");
		f.getParentFile().mkdirs();// 创建目录
		BufferedImage bi;
		try {
			bi = ImageIO.read(f);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write(bi, "png", baos);
			byte[] bytes = baos.toByteArray();

			return encoder.encodeBuffer(bytes).trim();
		} catch (IOException e) {
			e.printStackTrace();
			logger.info(e);
			return null;
		}
	}

	/*
	 * 保存条形码到图片的格式
	 */
	static void saveToPNG(String temp, BufferedImage paramBufferedImage,
			String paramString) {
		saveToFile(temp, paramBufferedImage, paramString, "png");
	}

	/*
	 * 保存图片到文件夹
	 */
	static void saveToFile(String temp, BufferedImage paramBufferedImage,
			String paramString1, String paramString2) {
		try {
			FileOutputStream localFileOutputStream = new FileOutputStream(temp
					+ "barcode/" + paramString1);
			ImageUtil.encodeAndWrite(paramBufferedImage, paramString2,
					localFileOutputStream, 84, 32);
			localFileOutputStream.close();
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

VelocityContext velocityContext = new VelocityContext();
<span style="white-space:pre">		</span>velocityContext.put("height", height);
<span style="white-space:pre">		</span>velocityContext.put("weight", weight);
<span style="white-space:pre">		</span>velocityContext.put("user_id", "MT" + user_id);
<span style="white-space:pre">		</span>velocityContext.put("picio", picio);
<span style="white-space:pre">		</span>velocityContext.put("createtime", createtime);
<span style="white-space:pre">		</span>velocityContext.put("bmi", bmi);
<span style="white-space:pre">		</span>velocityContext.put("starheight", starheight);
<span style="white-space:pre">		</span>velocityContext.put("conweight", conweight);
<span style="white-space:pre">		</span>velocityContext.put("sex", sex);
<span style="white-space:pre">		</span>velocityContext.put("name", name);
<span style="white-space:pre">		</span>velocityContext.put("age", age);
<span style="white-space:pre">		</span>velocityContext.put("rlist", rlist1);
<span style="white-space:pre">		</span>velocityContext.put("qplist", qplist);
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>ReportManageAction.createDoc(velocityContext, "crmreport1xml.vm",
<span style="white-space:pre">					</span>temp, user_id);
<span style="white-space:pre">			</span>ReportManageAction.createDocs(velocityContext, "crmreport2xml.vm",
<span style="white-space:pre">					</span>temp, user_id);
<span style="white-space:pre">			</span>ReportManageAction.deleteFile(temp);


	/*
	 * 生成word文档1
	 */
	public static void createDoc(VelocityContext vc, String templetePath,
			String temp, String user_id) throws Exception {
		Properties ps = new Properties();
		String temp1 = temp + "views\\survey\\";
		ps.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, temp1);// 这是模板所在路径
		VelocityEngine ve = new VelocityEngine();
		ve.init(ps);
		ve.init();
		Template template = ve.getTemplate(templetePath, "utf-8");
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy");
		SimpleDateFormat matter2 = new SimpleDateFormat("MM");
		SimpleDateFormat matter3 = new SimpleDateFormat("dd");
		File srcFile = new File("D:/report" + "/" + matter1.format(dt) + "/"
				+ matter2.format(dt) + "/" + matter3.format(dt) + "/1_"
				+ user_id + ".doc");// 输出路径
		srcFile.getParentFile().mkdirs();// 创建目录
		Writer out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(srcFile), "UTF-8"));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		/*
		 * FileOutputStream fos = new FileOutputStream(srcFile); BufferedWriter
		 * writer = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
		 */
		template.merge(vc, out);
		out.flush();
		out.close();
		// fos.close();
	}

	/*
	 * 生成word文档2
	 */
	public static void createDocs(VelocityContext vc, String templetePath,
			String temp, String user_id) throws Exception {
		Properties ps = new Properties();
		String temp1 = temp + "views\\survey\\";
		System.out.println(temp1);
		ps.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, temp1);// 这是模板所在路径
		VelocityEngine ve = new VelocityEngine();
		ve.init(ps);
		ve.init();
		Template template = ve.getTemplate(templetePath, "utf-8");
		Date dt = new Date();
		SimpleDateFormat matter1 = new SimpleDateFormat("yyyy");
		SimpleDateFormat matter2 = new SimpleDateFormat("MM");
		SimpleDateFormat matter3 = new SimpleDateFormat("dd");
		File srcFile = new File("D:/report" + "/" + matter1.format(dt) + "/"
				+ matter2.format(dt) + "/" + matter3.format(dt) + "/2_"
				+ user_id + ".doc");// 输出路径
		srcFile.getParentFile().mkdirs();// 创建目录
		Writer out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(srcFile)));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		/*
		 * FileOutputStream fos = new FileOutputStream(srcFile); BufferedWriter
		 * writer = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));
		 */
		template.merge(vc, out);
		out.flush();
		out.close();
		// fos.close();
	}

	/*
	 * 删除某一文件夹的文件
	 */
	public static void deleteFile(String temp) {
		File folder = new File(temp + "\\barcode");
		File[] files = folder.listFiles();
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			file.delete();
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值