参考にさせていただいたサイト
http://homepage3.nifty.com/anny/index.html
http://vapour.s22.xrea.com/javadojo/index.php?Java%A4%C7Excel
http://jexcelapi.sourceforge.net/
サンプル
import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.Number; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; public class ExcelEdit { public static void main(String[] args) { int line = 8; Workbook workbook = null; try { WorkbookSettings ws = new WorkbookSettings(); ws.setGCDisabled(true); workbook = Workbook.getWorkbook(new File("output.xls"), ws); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (workbook != null) { ; } } File outputFile= new File("output.xls"); WritableWorkbook workbook1=null; try { System.out.println(outputFile + "ブックを新規作成"); workbook1 = Workbook.createWorkbook(outputFile,workbook); if (workbook1 == null) { System.err.println(outputFile + "ブック作成に失敗"); } } catch (IOException ex) { System.err.println(ex.toString()); } WritableSheet sheet = workbook1.getSheet(0); if (sheet == null) { System.err.println(outputFile + "シート設定に失敗"); } Label label = new Label(1, line, "サンプル"); try { sheet.addCell(label); } catch (WriteException ex) { System.err.println("セルの書き込みに失敗" + ex.toString()); } Number number = new Number(0, line, 12800); try { sheet.addCell(number); } catch (WriteException ex) { System.err.println("line セルの書き込みに失敗" + ex.toString()); } System.out.println("日付セルを新規作成"); SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); String dateStr = format.format(new Date()); Label label2 = new Label(2, line, dateStr); try { sheet.addCell(label2); } catch (WriteException ex) { System.err.println("セルの書き込みに失敗" + ex.toString()); } try { workbook1.write(); } catch (IOException ex) { if (workbook1 != null) { System.err.println(outputFile + "ブックの書き込みに失敗" + ex.toString()); } } try { workbook1.close(); workbook.close(); } catch (Exception ex) { System.err.println(outputFile + "ブックのクローズに失敗" + ex.toString()); } } }
0 件のコメント:
コメントを投稿