基于Apache POI 向xlsx写入数据
生活随笔
收集整理的這篇文章主要介紹了
基于Apache POI 向xlsx写入数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【0】寫在前面
- 0.1) these codes are from 基于Apache POI 的向xlsx寫入數據
- 0.2) this idea is from http://cwind.iteye.com/blog/2187670 , adding some comments for easy understanding proves to be my work.
package com.cwind.poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;/*** @author Billy Chen*/ public class SimpleDatasheetWriter {private static final String[] titles = {"姓名", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};//sample data to fill the sheet.private static final String[][] data = {{"AngelaBaby", "跑了", "跑了", "跑了", "跑了", "跑了"},{"鄧超", "跑了", "跑了", "沒跑", "跑了", "跑了" },{"王祖藍", "沒跑", "沒跑", "沒跑", "跑了", "跑了" },{"王寶強", "跑了", "跑了", "跑了", "跑了", "跑了" },{"鄭愷", "跑了", "跑了", "跑了", "跑了", "跑了" }};public static void main(String[] args) throws Exception {Workbook wb;
//創建工作簿
if(args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook();elsewb = new XSSFWorkbook();//創建名為 Running Man 的紙張
Sheet sheet = wb.createSheet("Running Man");//創建行坐標為0的行(為什么管它叫坐標,不把它叫做行,呵呵)
Row headerRow = sheet.createRow(0);//設置行高
headerRow.setHeightInPoints(12.75f);for (int i = 0; i < titles.length; i++) {//創建行坐標為0的單元格,且其列坐標為i;
Cell cell = headerRow.createCell(i);//設置單元格的value
cell.setCellValue(titles[i]); }Row row;Cell cell;int rownum = 1;for (int i = 0; i < data.length; i++, rownum++) {//創建行坐標為rownum 的行
row = sheet.createRow(rownum); if(data[i] == null) continue;for (int j = 0; j < data[i].length; j++) {// 為行創建單元格;
cell = row.createCell(j);// 設置單元格的value
cell.setCellValue(data[i][j]); }}System.out.println("Default column width: " + sheet.getRow(0).getLastCellNum());System.out.println("Default column width: " + sheet.getRow(0).getPhysicalNumberOfCells());// Write the output to a file
String file = "E:/bench-cluster/temp-resource/RunningMan.xlsx";if(wb instanceof XSSFWorkbook) file += "x";FileOutputStream out = new FileOutputStream(file);wb.write(out);out.close(); // BufferedWriter bw = new BufferedWriter(new FileWriter(file));} }總結
以上是生活随笔為你收集整理的基于Apache POI 向xlsx写入数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 万网怎么续费(万网怎么续费优酷会员)
- 下一篇: 怎么申请邮箱域名(怎么申请邮箱域名注册)