jexcel导出excel文件的demo(二)

2014-11-24 08:07:34 · 作者: · 浏览: 1
9, fivedpsFontFormat);
sheet.addCell(number5);

/*
* Formatting Dates
*/
// Get the current date and time from the Calendar object
Date now = Calendar.getInstance().getTime();
DateFormat customDateFormat = new DateFormat ("dd MM yyyy hh:mm:ss");
WritableCellFormat dateFormat = new WritableCellFormat (customDateFormat);
DateTime dateCell = new DateTime(0, 6, now, dateFormat);
sheet.addCell(dateCell);

/*
* 合并单元格
*/
WritableSheet sheet1 = workbook.createSheet("First Sheet", 1);
sheet1.mergeCells(0, 0, 1, 1);

// All sheets and cells added. Now write out the workbook
workbook.write();
workbook.close();

System.out.println("创建成功..");

} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("创建失败..");
e.printStackTrace();
} catch (RowsExceededException e) {
// sheet.addCell Exception
System.out.println("创建失败..");
e.printStackTrace();
} catch (WriteException e) {
System.out.println("创建失败..");
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
*
* @param filePath 文件路径
* @param fileName 文件名
*/
public void ReadingSpreadSheets(String filePath,String fileName){
try {
Workbook workbook = Workbook.getWorkbook(new File(filePath + "\\" + fileName));
Sheet sheet = workbook.getSheet(0);

Cell a1 = sheet.getCell(0,0);
Cell b2 = sheet.getCell(1,1);
Cell c2 = sheet.getCell(2,1);

String stringa1 = a1.getContents();
String stringb2 = b2.getContents();
String stringc2 = c2.getContents();

double numberb2 = 0;
Date datec2 = null;

if (a1.getType() == CellType.LABEL)
{
LabelCell lc = (LabelCell) a1;
stringa1 = lc.getString();
}

if (b2.getType() == CellType.NUMBER)
{
NumberCell nc = (NumberCell) b2;
numberb2 = nc.getValue();
}

if (c2.getType() == CellType.DATE)
{
DateCell dc = (DateCell) c2;
datec2 = dc.getDate();
}

// 其他处理
// ...

// 结束后关闭文件并释放内存
workbook.close();
System.out.println("读取成功..");

} catch (BiffException e) {
// TODO Auto-generated catch block
System.out.println("读取失败..");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("读取失败..");
e.printStackTrace();
}

}

public void copyWorkBook(String oldFilePath,String oldFileName,String newFilePath,String newFileName ){
try {
Workbook workbook = Workbook.getWorkbook(new File( oldFilePath + "\\" + oldFileName ));
WritableWorkbook copy = Workbook.createWorkbook(new File( newFilePath +"\\" +newFileName ), workbook);

//进行一些操作
WritableSheet sheet = copy.getSheet(1);
WritableCell cell0 = sheet.getWritableCell(1, 2);

if (cell0.getType() == CellType.LABEL)
{
Label l0 = (Label) cell0;
l0.setString("modified cell");
}

WritableSheet sheet2 = copy.getSheet(1);
WritableCell cell = sheet2.getWritableCell(2, 4);

NumberFormat fivedps = new NumberFormat("#.#####");
WritableCellFormat cellFormat = new WritableCellFormat(fivedps);
cell.setCellFormat(cellFormat);

Label label = new Label(0, 2, "New label record");
sheet2.addCell(label);

Number number = new Number(3, 4, 3.1459);
sheet2.addCell(number);

// 所有的都完成,关闭资源
workbook.close();
copy.write();
copy.close();

} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {