从JAVA直接读取EXCEL、WORD并生成PDF文件

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/plane9999/article/details/83518852
从JAVA直接读取EXCEL、WORD并生成PDF文件

1。操作EXCEL和WORD文件
使用JAVA从EXCEL、WORD文件中读写数据,可以使用http://jexcelapi.sourceforge.net/提供的JAVA API-Java Excel API,这里有其指南http://www.andykhan.com/jexcelapi/tutorial.html,可由此下载JAR文件http://www.andykhan.com/jexcelapi/download.html。
此API提供所有的读、写、修改、建立的功能。对于一般应用足够了。在任何支持JAVA的系统上使用。目前,JExcelApi尚不支持有关chart、graph 或者 macro的信息,但是会拷贝并保留这些信息。只支持PNG图像。

2。生成PDF文件
若从JAVA生成PDF文件,可以使用http://www.qoppa.com/提供的应用程序库jPDFWriter。jPDFWriter是一个类库,可以直接从JAVA的应用程序调用以产生PDF文件。其网站的下载文件中包含了说明和例子,有兴趣可以仔细研究。


3。把JExcelAPI和jPDFWriter结合起来,就可以在应用中直接把XLS文件转换成PDF文件。如果再结合Database,就可以直接把有关的报告数据生成XLS文件的同时生成PDF文件。

下边给出了一个例子,是针对XLS,虽然简单,但是完整,以此为基础,可以按照自己的需要展开工作了。对于WORD也是一样,所有不再另给了。

//
//Compile:
//javac -classpath .;jexcelapi/jxl.jar;jPDFWriter/jPDFWriter.jar xlsPrinter.java
//Run:
//java -classpath .;jexcelapi/jxl.jar;jPDFWriter/jPDFWriter.jar xlsPrinter
//
//

import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import java.io.File;

import java.util.Vector;
import java.util.Date;

import jxl.*;//JExcelAPI

import com.qoppa.pdfPrinter.PDFPrinterJob;//jPDFWriter

public class xlsPrinter implements Printable
{
private int m_CurrentPage;
private int m_CurrentPageStartRow;
private int m_CurrentPageEndRow;
private final static int DEFAULT_COLUMN_WIDTH = 72;
private final static int CELL_MARGIN_X = 4;
private final static int CELL_MARGIN_Y = 4;

private Vector m_Data;
private int m_ColumnWidths [];
private boolean m_DrawGrid;

/**
* @param printString
*/
public xlsPrinter(Vector data, int [] colWidths, boolean drawGrid)
{
super();
m_Data = data;
m_ColumnWidths = colWidths;
m_DrawGrid = drawGrid;
}

//读取EXCEL数据并存入内存
private static Vector initData ()
{
Vector data = new Vector ();

try{
Workbook workbook = Workbook.getWorkbook(new File("output.xls"));
Sheet sheet = workbook.getSheet(0);
int rows = sheet.getRows();
int cols = sheet.getColumns();

// Initialize data
for (int row = 0; row < rows; ++row){
Vector rowData = new Vector ();
for (int col = 0; col < cols; ++col){
rowData.addElement (sheet.getCell(col,row).getContents());
}
data.addElement (rowData);
}

workbook.close();
}catch(Exception e){
e.printStackTrace();
}
return data;
}

//主程序
public static void main (String args [])
{
xlsPrinter gridPrinter = new xlsPrinter(initData (), null, true);

PrinterJob printerJob = PDFPrinterJob.getPrinterJob();
printerJob.setPrintable(gridPrinter);

try{
printerJob.print();
}catch (PrinterException pe){
pe.printStackTrace();
}

System.exit(0);
}

//打印接口,由此生成PDF文件,此段代码来源于jPDFWriter的例子
public int print (Graphics g, PageFormat pf, int pageIndex)
{
int lineHeight = g.getFontMetrics().getHeight();

// Reset current pos
int currentRow = 0;
if (pageIndex == 0)
{
// Need to do this in case the instance of this class
// gets used multiple times to print a string
m_CurrentPage = 0;
m_CurrentPageStartRow = 0;
}
// Need to do this because Java PrinterJob can call this
// method multiple times for the same page;
else if (m_CurrentPage == pageIndex)
{
currentRow = m_CurrentPageStartRow;
}
else
{
currentRow = m_CurrentPageEndRow + 1;
m_CurrentPageStartRow = currentRow;
}

// If we're out of lines, tell the PrinterJob we're done
if (currentRow >= m_Data.size())
{
return Printable.NO_SUCH_PAGE;
}

// Loop through lines until we fill the page
int currentY = (int)(pf.getImageableY() + lineHeight);
while (currentRow < m_Data.size() &&
currentY + lineHeight < pf.getImageableY() + pf.getImageableHeight())
{
// Draw the next line
int currentX = (int)pf.getImageableX();
Vector nextRow = (Vector)m_Data.elementAt (currentRow);
for (int col = 0; col < nextRow.size(); ++col)
{
String cellString = (String)nextRow.elementAt (col);
g.drawString (cellString, currentX + CELL_MARGIN_X, currentY + CELL_MARGIN_Y);

int colWidth = DEFAULT_COLUMN_WIDTH;
if (m_ColumnWidths != null && m_ColumnWidths.length > col)
{
colWidth = m_ColumnWidths [col];
}

// Draw grid if needed
if (m_DrawGrid)
{
g.drawRect (currentX, currentY - (lineHeight / 2), colWidth, lineHeight);
}

// Advance x
currentX += colWidth;
}

// Advance to the next line
++currentRow;
currentY += lineHeight;
}

// Save the ned line and current page
// Again, we have to do this because of multiple calls for the same page.
m_CurrentPageEndRow = currentRow;
m_CurrentPage = pageIndex;

return Printable.PAGE_EXISTS;
}
}
展开阅读全文

没有更多推荐了,返回首页