做一个测试,将list中的数据保存到excel文件中。部分代码给出,请问导出为excel的那部分该怎么实现呢
package ExportExcel;
import java.util.*;public class ExportExcelFile {
public void export(){
List userList = new ArrayList();
UserInfo userInfo = new UserInfo();
userInfo.setUserName("jenny");
userInfo.setEmail("[email protected]");
userInfo.setCompany("shewei");
userInfo.setAddress("yangpu");
userList.add(userInfo);
userList.add(userInfo);
userList.add(userInfo);
userList.add(userInfo);
userList.add(userInfo);
userList.add(userInfo);
System.out.println("userList size is "+userList.size());
}
public static void main(String[] args){
ExportExcelFile exportExcel = new ExportExcelFile();
exportExcel.export();
}
}
package ExportExcel;
import java.util.*;public class ExportExcelFile {
public void export(){
List userList = new ArrayList();
UserInfo userInfo = new UserInfo();
userInfo.setUserName("jenny");
userInfo.setEmail("[email protected]");
userInfo.setCompany("shewei");
userInfo.setAddress("yangpu");
userList.add(userInfo);
userList.add(userInfo);
userList.add(userInfo);
userList.add(userInfo);
userList.add(userInfo);
userList.add(userInfo);
System.out.println("userList size is "+userList.size());
}
public static void main(String[] args){
ExportExcelFile exportExcel = new ExportExcelFile();
exportExcel.export();
}
}
解决方案 »
- 请教一个窗口风格的问题
- java的三种注释:// 单行, /* */ 多行,/** */多行,怎样操作呢?
- java日期处理问题
- java如何打包及制作安装文件?
- 在JButton被按下的时候,有什么方法可以改变那个灰色的颜色吗?
- 作个小调查,请问你们在一个公司能呆多久,平均多久跳槽一次
- 学习编程-或许有点迷茫
- 请大家帮帮我
- class a里要引用class b里的方法,该方法为public,除了建类class b 的instance来引用该方法外还有什么办法引用???
- applet下如何用corba的命名服务,参数如何传递,急!(up有分)
- 关于串口
- 急!!ActionForward的实例是什么意思?
try {
short xlsrow = 0;
HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("sheet1");//建立新的sheet对象
HSSFCell cell = null;
HSSFRow row = null;
String [] xlshead = {"Logo_Message","Cust_Name","CUST_EMAIL","INVOICE_NO","ORD_DATE","Invoice_Message1","Invoice_Message2","Invoice_Message3","Invoice_Message4","Invoice_Message5","Invoice_Message6","Invoice_Message7","Invoice_Message8","Invoice_Message9","Addres_Informatio","Invoice Informatio","TOTAL","Total_TVA","Total_Charge","Total_HT","Currency","SKU","TranID","PRODNAME","QTY","PRICE","TVA","TOTAL_Cost"};
row=sheet.createRow(xlsrow);
HSSFCellStyle style=wb.createCellStyle();
//设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont font=wb.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short)12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font); for(short i=0; i<xlshead.length; i++) {
cell=row.createCell(i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(xlshead[i]);
cell.setCellStyle(style);
sheet.setColumnWidth((short)(i),(short)8000);
}
xlsrow++;
row=sheet.createRow(xlsrow);
String [] col_val = {"","","","","","","","","","","","","","","","",""};
while(rs.next()){
String invoiceMessage[] = new String[9];
StringBuffer addr = new StringBuffer("");
StringBuffer invoice = new StringBuffer("");
StringBuffer sku_sb = new StringBuffer("");
StringBuffer tranid_sb = new StringBuffer("");
StringBuffer prod_name_sb = new StringBuffer("");
StringBuffer qty_sb = new StringBuffer("");
StringBuffer price_sb = new StringBuffer("");
StringBuffer vat = new StringBuffer("");
StringBuffer prodValue = new StringBuffer("");//货值 qty=Integer.parseInt(rs.getString("qty"));
if(qty>5)
{
//空四行
row=sheet.createRow(xlsrow);
xlsrow++;
row=sheet.createRow(xlsrow);
xlsrow++;
row=sheet.createRow(xlsrow);
xlsrow++;
row=sheet.createRow(xlsrow);
}
int tempShort=0;
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("logo_message"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("Cust_Name"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("CUST_EMAIL"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("INVOICE_NO"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("ORD_DATE"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("Invoice_Message1"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("Invoice_Message2"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("Invoice_Message3"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("Invoice_Message4"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("Invoice_Message5"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("Invoice_Message6"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("Invoice_Message7"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("Invoice_Message8"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("Invoice_Message9"));
addr.append(rs.getString("cust_name")).append("</br>").append(rs.getString("cust_addr1")).append("</br>").append(rs.getString("cust_addr2")).append("</br>").append(rs.getString("cust_postcode")).append(",").append(rs.getString("cust_city"));
invoice.append(rs.getString("invoice_name")).append("</br>").append(rs.getString("invoice_addr1")).append("</br>").append(rs.getString("invoice_addr2")).append("</br>").append(rs.getString("invoice_zipcode")).append(",").append(rs.getString("invoice_city"));
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(addr.toString()); cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(invoice.toString()); String sales_tax=rs.getString("sales_tax");
BigDecimal rate = new BigDecimal(sales_tax);
rate=rate.multiply(new BigDecimal("0.01"));
BigDecimal totalCharge = new BigDecimal(rs.getString("totalPostCharge"));//总运费
BigDecimal totalCost = new BigDecimal(rs.getString("totalCost"));//总货值
BigDecimal total = totalCharge.add(totalCost);//总金额
BigDecimal totalRate = total.multiply(rate);//总税值(含运费)
BigDecimal bigvat = total.subtract(totalRate);//减税后金额
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(f.format(total)); cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(f.format(totalRate)); cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(f.format(totalCharge)); cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(f.format(bigvat)); ord_no = rs.getString("ord_no"); String strSQL2= " select prod_code,prod_name,transation_id,ord_qty,ord_price,payment_curr,convert(decimal(10,2),ord_price)*convert(decimal(10,2),ord_qty) as prodValue,convert(decimal(10,2),ord_price)*convert(decimal(10,2),ord_qty)*"+sales_tax+" as vat from so_orderdetail where ord_no=? order by ordd_no";
PreparedStatement pst=scon.prepareStatement(strSQL2);
pst.setString(1,ord_no);
ResultSet rs2=pst.executeQuery();
while(rs2.next()){
sku_sb.append(rs2.getString("prod_code")).append("</br>");
tranid_sb.append(rs2.getString("transation_id")).append("</br>");
String pname=rs2.getString("prod_name");
if(pname.length()>50)
{
pname=pname.substring(0,50);
}
prod_name_sb.append(pname).append("</br>");
qty_sb.append(rs2.getString("ord_qty")).append("</br>");
price_sb.append(rs2.getString("ord_price")).append("</br>");
vat.append(f.format(new BigDecimal(rs2.getString("vat")))).append("</br>");
prodValue.append(f.format(new BigDecimal(rs2.getString("prodValue")))).append("</br>");
currency = rs2.getString("payment_curr");
}
rs2.close();
pst.close();
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(currency); cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(sku_sb.toString()); cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(tranid_sb.toString()); cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(sku_sb.toString());
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(qty_sb.toString());
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(price_sb.toString());
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(vat.toString());
cell=row.createCell((short)tempShort++);cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(prodValue.toString()); xlsrow++;
row=sheet.createRow(xlsrow);
out_num++; }
//FileOutputStream fileOut = new FileOutputStream("c:\\a.xls");
//wb.write(fileOut);
//fileOut.close();
wb.write(response.getOutputStream());
} catch (Exception ex) {
ex.printStackTrace();
}