java 导出csv大数据_每日一课 | Java 将数据从数据库导出到 PDF

8c2a51d7bc15c82c4cdff3d3b33caae6.gif 这篇文章将向您展示如何通过使用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 coordinatefloat 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) titleFont.measureString("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");
    }
}
这是结果文件的屏幕截图 1a0d5c30dded86b33f6d700f4764a388.png 近景 1b8b2d0c402480fad69f9e2bce5031d5.png 原文链接: https://dev.to//eiceblue/export-data-from-database-to-pdf-in-java-874 推荐阅读--

当一个程序员飞速敲键盘时,他在干嘛?

卧槽!最新编程语言排名,Java 沦为老二。。

为何程序员上班时间总戴个耳机,看完恍然大悟...

每日一课 | 如何将Java对象读取和写入文件

每日一课 | Java Final关键字示例

43bfe52f-8a33-eb11-8da9-e4434bdf6706.svg

球分享

47bfe52f-8a33-eb11-8da9-e4434bdf6706.svg

球点赞

1fa0b9f84b2c92f89a246a2f98ca6985.gif

球在看

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值