用 Java 将数据从数据库导出到 PDF

您所在的位置:网站首页 数据库怎么导出数据格式 用 Java 将数据从数据库导出到 PDF

用 Java 将数据从数据库导出到 PDF

2024-03-01 00:57| 来源: 网络整理| 查看: 265

这篇文章将向您展示如何通过使用Java的Free Spire.PDF将 Microsoft Access数据库中的数据导出为表格的PDF 。此外,本文还将解决一些相关问题,如下所示。

如果表格超过一页,如何分页?如何格式化表格,例如,如何设置交替的行颜色,以及如何在单元格内对齐文本?如何检测表格的底部,以便我们可以在下面添加其他内容?

注意:JDBC-ODBC桥已从Java SE 8中删除,不再受支持。要使用Java连接到Access数据库,可以使用Java SE 7或更低版​​本,否则,您将需要使用UCanAccess之类的东西来代替JDBC。该示例将JDK 6与JDBC一起使用没有任何问题。

使用代码 import com.spire.pdf.graphics.*; import com.spire.pdf.tables.*; import com.spire.pdf.tables.table.*; import com.spire.pdf.tables.table.common.JdbcAdapter; import java.awt.geom.*; import java.awt.*; import java.sql.*; public class ExportDataToPdf { public static void main(String[] args) { //create a PDF document PdfDocument doc = new PdfDocument(); //set page margins doc.getPageSettings().setMargins(30f,30f,30f,30f); //add a page PdfPageBase page = doc.getPages().add(); //initialize y coordinate float y = 0; //create a brush PdfBrush brush = PdfBrushes.getBlack(); //create four types of fonts PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 16)); PdfTrueTypeFont tableFont= new PdfTrueTypeFont(new Font("Arial", 0, 10)); PdfTrueTypeFont headerFont= new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 11)); PdfTrueTypeFont textFont= new PdfTrueTypeFont(new Font("Arial", 0, 12)); //draw title on the center of the page PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center); page.getCanvas().drawString("Employee Table", titleFont, brush, page.getCanvas().getClientSize().getWidth() / 2, y, format); //calculate y coordinate y = y + (float) titleFonasureString("Employee Table", format).getHeight(); y = y + 5; //create a PdfTable instance PdfTable table = new PdfTable(); //set the default cell style and row style table.getStyle().setCellPadding(2); table.getStyle().setBorderPen(new PdfPen(brush, 0.75f)); table.getStyle().getDefaultStyle().setBackgroundBrush(PdfBrushes.getWhite()); table.getStyle().getDefaultStyle().setFont(tableFont); table.getStyle().getDefaultStyle().setStringFormat(new PdfStringFormat(PdfTextAlignment.Center)); table.getStyle().getAlternateStyle().setBackgroundBrush(PdfBrushes.getLightGray()); table.getStyle().getAlternateStyle().setFont(tableFont); table.getStyle().getAlternateStyle().setStringFormat(new PdfStringFormat(PdfTextAlignment.Center)); //set the header style table.getStyle().setHeaderSource(PdfHeaderSource.Column_Captions); table.getStyle().getHeaderStyle().setBackgroundBrush(PdfBrushes.getPurple()); table.getStyle().getHeaderStyle().setFont(headerFont); table.getStyle().getHeaderStyle().setTextBrush(PdfBrushes.getWhite()); table.getStyle().getHeaderStyle().setStringFormat(new PdfStringFormat(PdfTextAlignment.Center)); //show header at every page table.getStyle().setShowHeader(true); //connect to database String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + "C:\\Users\\Administrator\\Documents\\data.mdb"; DataTable dataTable = new DataTable(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); try { Connection conn = DriverManager.getConnection(url); Statement sta = conn.createStatement(); ResultSet resultSet = sta.executeQuery("select * from employee "); JdbcAdapter jdbcAdapter = new JdbcAdapter(); //export data from database to datatable jdbcAdapter.fillDataTable(dataTable, resultSet); table.setDataSourceType(PdfTableDataSourceType.Table_Direct); //fill the table with datatable table.setDataSource(dataTable); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } //paginate table PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat(); tableLayout.setLayout(PdfLayoutType.Paginate); //draw table at the specified x, y coordinates PdfLayoutResult result = table.draw(page, new Point2D.Float(0, y), tableLayout); //calculate y coordinate y = (float) result.getBounds().getHeight() + 5; //draw text under the table result.getPage().getCanvas().drawString(String.format("* %1$s employees in the list.", table.getRows().getCount()), textFont, brush, 5, y); //save pdf file. doc.saveToFile("ExportDataToPdf.pdf"); } }

这是结果文件的屏幕截图。 Format text content 近景。Format text content



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3