csv到pdf文件中 [英] csv to pdf file in java

查看:185
本文介绍了csv到pdf文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得 csv 文件解析为 pdf 。我到目前为止附加在下面。

I am trying to get a csv file parsed into a pdf. What I have so far is attached below.

我的问题是这个代码的文件,最后在pdf截断在csv文件的第一行,我不知道为什么。 [附加的示例]基本上我想要一个pdf版本的csv文件没有操纵。我相信它的一个问题,我如何添加数据到itext pdf,但我不能真正找到另一种方法转发数组到pdf文件。任何想法修复代码或更简单的东西?

My problem is with this code the file that ends up in the pdf cuts off at the first line of the csv file and I cant figure out why. [examples attached] Essentially I would like a pdf version of the csv file nothing manipulated. I am sure its an issue with how i am adding data to the itext pdf but I cant really find another way of forwarding an array to the pdf file. Any ideas on fixing the code or something simpler?

public static void main(String[] args) throws IOException, DocumentException {
    @SuppressWarnings("resource")
    CSVReader reader = new CSVReader(new FileReader("data1.csv") ,'\'');
    String [] nextLine;
    while ((nextLine = reader.readNext()) != null) {
            // nextLine[] is an array of values from the line
            System.out.println(nextLine[0]);
            String test; 
            test = nextLine[0];

            // step 1
            Document document = new Document();

            // step 2
            PdfWriter.getInstance(document, new FileOutputStream("Test.pdf"));

            // step 3
            document.open();

            // step 4
            PdfPTable arrayTable3 = new PdfPTable(1); 
            arrayTable3.setHorizontalAlignment(Element.ALIGN_LEFT); 

            Phrase phrase1 = new Phrase(nextLine[0]); 
            PdfPCell arrayDetailsCell3 = new PdfPCell(); 

            arrayDetailsCell3.addElement(phrase1); 

            // Add the cell to the table 
            arrayTable3.addCell(arrayDetailsCell3); 

            // Add table to the document 
            document.add(arrayTable3); 

            // step 5
            document.close();
    }
}

CSV档案: http://dl.dropbox.com/u/11365830/data1.csv

PDF文件: http://dl.dropbox.com/u/11365830/Test.pdf

推荐答案

在CSVReader.java中将DEFAULT_SKIP_LINES设置为0

set DEFAULT_SKIP_LINES to 0 in CSVReader.java

这篇关于csv到pdf文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆