日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于Apache POI 向xlsx写入数据

發布時間:2023/12/3 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于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写入数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。