|
发表于 2012-3-29 14:13:55
|
显示全部楼层
可以用原来的按钮,也可以新增。其实原来的按钮就可以,原来的按钮是没有实现方法。
给你个例子,详细的自己研究
/ 导出方法
@SuppressWarnings("deprecation")
private void OutData() {
// TODO Auto-generated method stub 客商编码、名称、额度、月份
int returnVal = getFileChooser().showSaveDialog(this);
XyedVO xvo = (XyedVO) getBillCardPanel().getBillValueVO(
XyedVO.class.getName(), XyedzVO.class.getName(),
XyedbVO.class.getName());
try {
FileOutputStream fileOut = null;
String month = (String) xvo.getParentVO()
.getAttributeValue("month");
String year = (String) xvo.getParentVO().getAttributeValue("year");
int rowCount = getBillCardPanel().getBillModel().getRowCount();
if (returnVal == JFileChooser.APPROVE_OPTION) {
String Description = getFileChooser().getFileFilter()
.getDescription();
HSSFWorkbook wb = null;
if (Description.equals("Microsoft Office Excel 工作簿 (.xls)")) {
wb = new HSSFWorkbook();
fileOut = new FileOutputStream(getFileChooser()
.getSelectedFile()
+ ".xls");
}
HSSFSheet sheet = wb.createSheet("导出文件");
// 设置列宽
sheet.setColumnWidth((short)0, (short)6000);
sheet.setColumnWidth((short)1, (short)6000);
sheet.setColumnWidth((short)2, (short)6000);
sheet.setColumnWidth((short)3, (short)6000);
HSSFRow row = sheet.createRow(1);
row.createCell((short) 0).setCellValue("年份");
row.createCell((short)2).setCellValue("月份");
row.createCell((short)1).setCellValue(Integer.parseInt(year));
row.createCell((short)3).setCellValue(Integer.parseInt(month));
HSSFRow row1 = sheet.createRow(2);
row1.createCell((short)0).setCellValue("客商编码");
row1.createCell((short)1).setCellValue("客商名称");
row1.createCell((short)2).setCellValue("账期");
row1.createCell((short)3).setCellValue("信用额度");
for (int i = 0; i < rowCount; i++) {
HSSFRow rows = sheet.createRow(3 + i);
String custcode = (String) getBillCardPanel()
.getBillModel().getValueAt(i, "custcode");
String custname = (String) getBillCardPanel()
.getBillModel().getValueAt(i, "custname");
String term = (String) getBillCardPanel().getBillModel()
.getValueAt(i, "term");
String credit = (String) getBillCardPanel().getBillModel()
.getValueAt(i, "credit");
if (credit != null) {
rows.createCell((short)3).setCellValue(credit);
}
if (term != null) {
rows.createCell((short)2).setCellValue(term.trim());
}
rows.createCell((short)0).setCellValue(custcode);
rows.createCell((short)1).setCellValue(custname);
}
wb.write(fileOut);
fileOut.close();
showHintMessage("导出完成!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
showErrorMessage(e.getMessage());
e.printStackTrace();
}
}
public JFileChooser getFileChooser() {
if (fileChooser == null) {
fileChooser = new JFileChooser();
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("xls");
filter.setDescription("Microsoft Office Excel 工作簿");
fileChooser.setFileFilter(filter);
}
return fileChooser;
}
|
|