1.需要掛載itextpdf.jar檔
2.準備好CSV檔
3.程式碼
import java.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import com.itextpdf.text.Document.*;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
class loadCSV
{
static String filePath="D:\\LoadCSV";
static String fileName="";
static Vector<String[]> vData = new Vector<String[]>();
public static void main(String[] args) throws Throwable
{
File f = new File(filePath);
File[] arrF = f.listFiles();
for(int i = 0; i < arrF.length; i++) {
if (arrF[i].getName().toUpperCase().indexOf(".CSV") != -1) {
fileName = arrF[i].getName();
}
}
System.out.println(fileName);
File fr = new File(filePath + "/" + fileName);
InputStreamReader read = new InputStreamReader (new FileInputStream(fr), "big5");
BufferedReader br = new BufferedReader(read);
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
line = line.replace("\",\"", "@split@");
String[] arrLine = line.split("@split@");
String meid = arrLine[0];
System.out.println("========"+meid);
String name=arrLine[1];
String tel=arrLine[2];
vData.add(new String[]{meid,name,tel});
}
read.close();
br.close();
System.out.println(vData.size());
Document document = new Document(PageSize.A4, 4f, 0f, 50f, 0f);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath + "/tmp" + ".pdf"));
document.open();
document.newPage();
//建立PdfPTable物件並設定其欄位數
PdfPTable table = new PdfPTable(2);
//設定table的寬度
table.setWidthPercentage(100f);
//設定每個欄位的寬度
table.setWidths(new float[]{0.20f, 0.20f});
PdfPCell title = new PdfPCell(new Phrase("Table's Title"));
//合併儲存格
title.setColspan(2);
//水平置中
title.setHorizontalAlignment(Element.ALIGN_CENTER);
//沒有外框線
title.setBorder(Rectangle.NO_BORDER);
table.addCell(title);
for(int i=1;i<vData.size();i++){
String[] idata = vData.get(i);
PdfPCell cell_1 = new PdfPCell();
for(int x=0;x<idata.length ; x++){
if((vData.size()-1)%2!=0&&i==vData.size()-1){
cell_1.setColspan(2);
}
cell_1.addElement(new Phrase(idata[x]));
}
table.addCell(cell_1);
}
// //設定第一個欄位的內容
// PdfPCell cell_1 = new PdfPCell();
// cell_1.addElement(new Phrase("Column 1"));
// cell_1.addElement(new Phrase("Column 1"));
// table.addCell(cell_1);
//
//
// //設定第二個欄位的內容
// PdfPCell cell_2 = new PdfPCell();
// cell_2.addElement(new Phrase("Column 2"));
// cell_2.addElement(new Phrase("Column 2"));
// table.addCell(cell_2);
document.add(table);
document.close();
writer.close();
}
}