######導出Excle########
public InputStream getTableTransaction() throws FileNotFoundException {
Date datenow=new Date();
SimpleDateFormat dateFormatNow=new SimpleDateFormat ("yyyy-M-d-HH-mm-ss");
String dateNow=dateFormatNow.format(datenow);
String path=ServletActionContext.getServletContext().getRealPath("/DatExport").replace("/", "\\")+"\\"+dateNow+".csv"; //路徑
File f=new File(path);
HttpSession session = ServletActionContext.getRequest().getSession();
saveTransactionList=(List<invoice>)session.getAttribute("list");
try
{
if(f.createNewFile())
{
String Contxt=new String();
String line="Invoice Number, Invoice Date,Invoice Status,Client,Start Date,End Date,Due Date,Currency,Invoice Amount,PST," + //表頭
"GST,QST,HST,Discount Amounts"+"\r\n";
Contxt=line;
if(saveTransactionList!=null)
{
for(int i=0;i<saveTransactionList.size();i++)
{
invoice st=saveTransactionList.get(i);
line=st.getInvoicenumber()+","+st.getInvoicedate()+","+st.getInvoicestatus()+","+st.getClientname()+","+st.getStartdate()+","+st.getEnddate() //內容
+","+st.getDuedate()+","+st.getCurrency()+","+st.getInvoiceamount()+","+st.getPst()+","+st.getGst()+","+st.getQst()
+","+st.getHst()+","+st.getDisc_amount()+"\r\n";
Contxt+=line;
}
}
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(Contxt);
output.close();
}
}catch (IOException e)
{
e.printStackTrace();
}
tableTransaction=new java.io.FileInputStream(path);
return tableTransaction;
}
struts 配置
<action name="searchInvoiceAction*" class="com.hruisoft.www.action.Invoice"
method="{1}">
<result name="success">/invoice.jsp</result>
<result name="fail">/login.jsp</result>
<result name="ExportToCsv" type="stream">
<param name="contentType">text/plain</param>
<param name="inputName">tableTransaction</param>
<param name="contentDisposition">attachment;filename="report.csv"</param>
<param name="bufferSize">4096</param>
</result>
</action>
######導出PDF########
public InputStream getPdfTable() throws FileNotFoundException {
Date datenow=new Date();
SimpleDateFormat dateFormatNow=new SimpleDateFormat ("yyyy-M-d-HH-mm-ss");
String dateNow=dateFormatNow.format(datenow);
String path=ServletActionContext.getServletContext().getRealPath("/DatExport").replace("/", "\\")+"\\"+dateNow+".pdf";
try
{
HttpSession session = ServletActionContext.getRequest().getSession();
String reportType="";
if(session.getAttribute("reportType")!=null)
reportType=(String)session.getAttribute("reportType");
Document document = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(path));
HeaderFooter footer = new HeaderFooter(new Phrase("Page ",new Font(Font.HELVETICA, 6)), true);
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(0);
document.setFooter(footer);
document.open();
PdfPTable table = new PdfPTable(14); //設置列數
PdfPCell pcell1 = new PdfPCell(new Phrase("Card #", new Font(Font.TIMES_ROMAN, 6, Font.BOLD,Color.BLACK)));
pcell1.setBackgroundColor(Color.LIGHT_GRAY);
pcell1.setHorizontalAlignment(2);
PdfPCell pcell2 = new PdfPCell(new Phrase("Unit #", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell2.setBackgroundColor(Color.LIGHT_GRAY);
pcell2.setHorizontalAlignment(2);
PdfPCell pcell3 = new PdfPCell(new Phrase("Driver Name", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell3.setBackgroundColor(Color.LIGHT_GRAY);
pcell3.setHorizontalAlignment(2);
PdfPCell pcell4 = new PdfPCell(new Phrase("DATE", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell4.setBackgroundColor(Color.LIGHT_GRAY);
pcell4.setHorizontalAlignment(2);
PdfPCell pcell5 = new PdfPCell(new Phrase("TIME", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell5.setBackgroundColor(Color.LIGHT_GRAY);
pcell5.setHorizontalAlignment(2);
PdfPCell pcell6 = new PdfPCell(new Phrase("Site Number", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell6.setBackgroundColor(Color.LIGHT_GRAY);
pcell6.setHorizontalAlignment(2);
PdfPCell pcell7 = new PdfPCell(new Phrase("Site Name", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell7.setBackgroundColor(Color.LIGHT_GRAY);
pcell7.setHorizontalAlignment(2);
PdfPCell pcell9 = new PdfPCell(new Phrase("Province/State", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell9.setBackgroundColor(Color.LIGHT_GRAY);
pcell9.setHorizontalAlignment(2);
PdfPCell pcell10 = new PdfPCell(new Phrase("Tractor Fuel", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell10.setBackgroundColor(Color.LIGHT_GRAY);
pcell10.setHorizontalAlignment(2);
PdfPCell pcell11 = new PdfPCell(new Phrase("Reefer Fuel", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell11.setBackgroundColor(Color.LIGHT_GRAY);
pcell11.setHorizontalAlignment(2);
PdfPCell pcell12 = new PdfPCell(new Phrase("Fuel QTY", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell12.setBackgroundColor(Color.LIGHT_GRAY);
pcell12.setHorizontalAlignment(2);
PdfPCell pcell13 = new PdfPCell(new Phrase("Litre or Gallon", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcell13.setBackgroundColor(Color.LIGHT_GRAY);
pcell13.setHorizontalAlignment(2);
PdfPCell pcellPFT = new PdfPCell(new Phrase("Previous Odometer ", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcellPFT.setBackgroundColor(Color.LIGHT_GRAY);
pcellPFT.setHorizontalAlignment(2);
PdfPCell pcellHET = new PdfPCell(new Phrase("Odometer", new Font(Font.TIMES_ROMAN, 6, Font.BOLD, Color.BLACK)));
pcellHET.setBackgroundColor(Color.LIGHT_GRAY);
pcellHET.setHorizontalAlignment(2);
table.addCell(pcell1);
table.addCell(pcell2);
table.addCell(pcell3);
table.addCell(pcell4);
table.addCell(pcell5);
table.addCell(pcell6);
table.addCell(pcell7);
table.addCell(pcell9);
table.addCell(pcell10);
table.addCell(pcell11);
table.addCell(pcell12);
table.addCell(pcell13);
table.addCell(pcellPFT);
table.addCell(pcellHET);
saveReportList=(List<report>)session.getAttribute("list");
if(saveReportList!=null)
{
Font pdfCellFont=new Font(Font.TIMES_ROMAN, 5,Font.NORMAL,Color.BLACK); //All cell font format
for(int i=0;i<saveReportList.size();i++)
{
report st=saveReportList.get(i);
table.addCell(new Phrase(st.getCardnumber(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getUnitnumber(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getDrivername(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getDate(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getTime(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getSitenumber(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getSitename(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getState(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getTractorfuel(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getReferfuel(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getFuelqty(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getUom(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getPreviousOdometer(),pdfCellFont));//, font));
table.addCell(new Phrase(st.getOdometer(),pdfCellFont));//, font));
}
}
document.add(table);
document.close();
writer.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
pdfTable=new java.io.FileInputStream(path);
return pdfTable;
}
struts 配置與上面類似