⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reportengine.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
							tr.addElement(td);
							Object obj = m_printData.getNode(new Integer(item.getAD_Column_ID()));
							if (obj == null)
								td.addElement(" ");
							else if (obj instanceof PrintDataElement)
							{
								String value = ((PrintDataElement)obj).getValueDisplay(language);	//	formatted
								td.addElement(Util.maskHTML(value));
							}
							else if (obj instanceof PrintData)
							{
								//	ignore contained Data
							}
							else
								log.error("createHTML - Element not PrintData(Element) " + obj.getClass());
						}
					}	//	printed
				}	//	for all columns
			}	//	for all rows

			//
			PrintWriter w = new PrintWriter(writer);
			if (onlyTable)
				table.output(w);
			else
			{
				XhtmlDocument doc = new XhtmlDocument();
				doc.appendBody(table);
				doc.output(w);
			}
			w.flush();
			w.close();
		}
		catch (Exception e)
		{
			log.error("createHTML(w)", e);
		}
		return false;
	}	//	createHTML


	/*************************************************************************/

	/**
	 * 	Create CSV File
	 * 	@param file file
	 *  @param delimiter delimiter, e.g. comma, tab
	 *  @param language translation language
	 * 	@return true if success
	 */
	public boolean createCSV (File file, char delimiter, Language language)
	{
		try
		{
			FileWriter fw = new FileWriter (file, false);
			return createCSV (new BufferedWriter(fw), delimiter, language);
		}
		catch (FileNotFoundException fnfe)
		{
			log.error("createCSV(f) - " + fnfe.toString());
		}
		catch (Exception e)
		{
			log.error("createCSV(f)", e);
		}
		return false;
	}	//	createCSV

	/**
	 * 	Write CSV to writer
	 * 	@param writer writer
	 *  @param delimiter delimiter, e.g. comma, tab
	 *  @param language translation language
	 * 	@return true if success
	 */
	public boolean createCSV (Writer writer, char delimiter, Language language)
	{
		if (delimiter == 0)
			delimiter = '\t';
		try
		{
			//	for all rows (-1 = header row)
			for (int row = -1; row < m_printData.getRowCount(); row++)
			{
				StringBuffer sb = new StringBuffer();
				if (row != -1)
					m_printData.setRowIndex(row);

				//	for all columns
				boolean first = true;	//	first column to print
				for (int col = 0; col < m_printFormat.getItemCount(); col++)
				{
					MPrintFormatItem item = m_printFormat.getItem(col);
					if (item.isPrinted())
					{
						//	column delimiter (comma or tab)
						if (first)
							first = false;
						else
							sb.append(delimiter);
						//	header row
						if (row == -1)
							createCSVvalue (sb, delimiter,
								m_printFormat.getItem(col).getPrintName(language));
						else
						{
							Object obj = m_printData.getNode(new Integer(item.getAD_Column_ID()));
							String data = "";
							if (obj == null)
								;
							else if (obj instanceof PrintDataElement)
							{
								data = ((PrintDataElement)obj).getValueDisplay(null);	//	not formatted
							}
							else if (obj instanceof PrintData)
							{
							}
							else
								log.error("createCSV - Element not PrintData(Element) " + obj.getClass());
							createCSVvalue (sb, delimiter, data);
						}
					}	//	printed
				}	//	for all columns
				writer.write(sb.toString());
				writer.write(Env.NL);
			}	//	for all rows
			//
			writer.flush();
			writer.close();
		}
		catch (Exception e)
		{
			log.error("createCSV(w)", e);
		}
		return false;
	}	//	createCSV

	/**
	 * 	Add Content to CSV string.
	 *  Encapsulate/mask content in " if required
	 * 	@param sb StringBuffer to add to
	 * 	@param delimiter delimiter
	 * 	@param content column value
	 */
	private void createCSVvalue (StringBuffer sb, char delimiter, String content)
	{
		//	nothing to add
		if (content == null || content.length() == 0)
			return;
		//
		boolean needMask = false;
		StringBuffer buff = new StringBuffer();
		char chars[] = content.toCharArray();
		for (int i = 0; i < chars.length; i++)
		{
			char c = chars[i];
			if (c == '"')
			{
				needMask = true;
				buff.append(c);		//	repeat twice
			}	//	mask if any control character
			else if (!needMask && (c == delimiter || !Character.isLetterOrDigit(c)))
				needMask = true;
			buff.append(c);
		}

		//	Optionally mask value
		if (needMask)
			sb.append('"').append(buff).append('"');
		else
			sb.append(buff);
	}	//	addCSVColumnValue

	/*************************************************************************/

	/**
	 * 	Create XML File
	 * 	@param file file
	 * 	@return true if success
	 */
	public boolean createXML (File file)
	{
		try
		{
			FileWriter fw = new FileWriter (file, false);
			return createXML (new BufferedWriter(fw));
		}
		catch (FileNotFoundException fnfe)
		{
			log.error("createXML(f) - " + fnfe.toString());
		}
		catch (Exception e)
		{
			log.error("createXML(f)", e);
		}
		return false;
	}	//	createXML

	/**
	 * 	Write XML to writer
	 * 	@param writer writer
	 * 	@return true if success
	 */
	public boolean createXML (Writer writer)
	{
		try
		{
			m_printData.createXML(new StreamResult(writer));
			writer.flush();
			writer.close();
		}
		catch (Exception e)
		{
			log.error("createXML(w)", e);
		}
		return false;
	}	//	createXML

	/*************************************************************************/

	/**
	 * 	Create PDF file.
	 * 	(created in temporary storage)
	 *	@return PDF file
	 */
	public File getPDF()
	{
		return getPDF(null);
	}	//	getPDF

	/**
	 * 	Create PDF file.
	 * 	@param file file
	 *	@return PDF file
	 */
	public File getPDF (File file)
	{
		if (file == null)
		{
			try
			{
				file = File.createTempFile ("ReportEngine", ".pdf");
			}
			catch (Exception ex)
			{
				log.error("getFile", ex);
			}
		}
		if (createPDF (file))
			return file;
		return null;
	}	//	getPDF

	/**
	 * 	Create PDF File
	 * 	@param file file
	 * 	@return true if success
	 */
	public boolean createPDF (File file)
	{
		try
		{
			if (file == null)
				file = File.createTempFile ("ReportEngine", ".pdf");

			//	get format
			if (m_layout == null)
				layout ();
			Print print = new Print (m_layout, false);

			//	Paper Attributes: 	media-printable-area, orientation-requested, media
			PrintRequestAttributeSet prats = m_layout.getPaper ().
											 getPrintRequestAttributeSet ();
			prats.add (new JobName (file.getAbsolutePath (), Locale.getDefault ()));
			//	Destination
			prats.add (new Destination (file.toURI ()));

			/**
			 * 	You can get a demo version via
			 * 	http://www.qoppa.com/demo/jPDFPrinter.jar
			 *  The full version is included in the ComPiere Support,
			 *  and you can get it also from  http://www.qoppa.com
			 */
			if (false)
				Class.forName("com.qoppa.pdfPrinter.PDFPrinterJob");
			//
			PDFPrinterJob job = (PDFPrinterJob)PDFPrinterJob.getPrinterJob ();
			//	job.getPrintService().addPrintServiceAttributeListener(this);
			job.setPrintable(print, m_layout.getPageFormat());
			log.debug("createPDF - " + file.getAbsolutePath() + " - " + file.length());
			job.print(file.getAbsolutePath());
			log.info("createPDF - " + file.getAbsolutePath() + " - " + file.length());
			/**	End Quoppa stuff		**/

			/**
			 * 	Alternatively to getting a license, you could use this
			 *
			//	Search for PrintService (but w/o attributes)
			PrintService[] pss =
				PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, new HashPrintRequestAttributeSet());
			boolean printed = false;
			for (int i = 0; !printed && i < pss.length; i++)
			{
				PrintService ps = pss[i];
				String name = ps.getName();
				if (name.indexOf("PDF") != -1)
				{
					log.info("createPDF", name);
					ps.createPrintJob().print(print, prats);
				//	PrintUtil.dump(prats);
					printed = true;		//	take first
				}
			}
			if (!printed)
				log.error("createPDF - NO PDF Printer");
			/**	End PrintService Stuff	**/

			return true;
		}
		catch (ClassNotFoundException cnf)
		{
			log.error("Download http://www.qoppa.com/demo/jPDFPrinter.jar  and save it in the lib directory");
		}
		catch (Exception e)
		{
			log.error("createPDF", e);
		}
		//	Error
		return false;
	}	//	createPDF

	/*************************************************************************/

	/**
	 * 	Create PostScript File
	 * 	@param file file
	 * 	@return true if success
	 */
	public boolean createPS (File file)
	{
		try
		{
			return createPS (new FileOutputStream(file));
		}
		catch (FileNotFoundException fnfe)
		{
			log.error("createPS(f) - " + fnfe.toString());
		}
		catch (Exception e)
		{
			log.error("createPS(f)", e);
		}
		return false;
	}	//	createPS

	/**
	 * 	Write PostScript to writer
	 * 	@param fos file output stream
	 * 	@return true if success
	 */
	public boolean createPS (FileOutputStream fos)
	{
		try
		{
			String outputMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
			DocFlavor docFlavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
			StreamPrintServiceFactory[] spsfactories =
				StreamPrintServiceFactory.lookupStreamPrintServiceFactories(docFlavor, outputMimeType);
			if (spsfactories.length == 0)
			{
				log.error("createPS(fos) - No StreamPrintService");
				return false;
			}
			//	just use first one - sun.print.PSStreamPrinterFactory
			//	System.out.println("- " + spsfactories[0]);
			StreamPrintService sps = spsfactories[0].getPrintService(fos);
			//	get format
			if (m_layout == null)
				layout();
			Print print = new Print (m_layout, false);
			//	print it
			sps.createPrintJob().print(print, new HashPrintRequestAttributeSet());
			//
			fos.flush();
			fos.close();
		}
		catch (Exception e)
		{
			log.error("createPS(fos)", e);
		}
		return false;
	}	//	createPS


	/*************************************************************************/

	/**
	 * 	Test
	 * 	@param args args
	 */
	public static void main(String[] args)
	{
		org.compiere.Compiere.startupClient();
		//
		MQuery q = new MQuery("AD_Table");
		q.addRestriction("AD_Table_ID", "<", 108);
		int AD_PrintFormat_ID = 133;					//	Need to create Print Format for AD_Table !!
		MPrintFormat f = MPrintFormat.get (AD_PrintFormat_ID, true);
		ReportEngine re = new ReportEngine(Env.getCtx(), f, q);
		re.layout();

		re.createCSV(new File("C:\\Temp\\test.csv"), ',', Language.getLanguage());
		re.createHTML(new File("C:\\Temp\\test.html"), false, Language.getLanguage());
		re.createXML(new File("C:\\Temp\\test.xml"));
		re.createPS(new File ("C:\\Temp\\test.ps"));
		re.createPDF(new File("C:\\Temp\\test.pdf"));
		re.print(true, 1, false);
		System.exit(0);
	}
}	//	ReportEngine

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -