groovy : poi 匯出 Excel xlsx,poixlsx

來源:互聯網
上載者:User

groovy : poi 匯出 Excel xlsx,poixlsx

write_xlsx.groovy 代碼如下

package xlsx;import javax.swing.JFileChooser;import javax.swing.filechooser.FileFilter;import javax.swing.filechooser.FileNameExtensionFilter;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import java.math.BigDecimal;import java.util.List;import org.apache.poi.xssf.usermodel.XSSFDataFormat;import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;class Text2xlsx {private static String getFilename(String initDir) {if (initDir==null || initDir=='') initDir='.';def chooser=new JFileChooser(initDir)def filter = new FileNameExtensionFilter('Text', 'txt')chooser.setFileFilter(filter)chooser.setDialogTitle("Open")if(chooser.showSaveDialog() == JFileChooser.APPROVE_OPTION)return chooser.getSelectedFile()else if(chooser.showSaveDialog() == JFileChooser.CANCEL_OPTION)return nullelsereturn null}void write_xlsx(String filename) {if (filename==null || filename=='') return;def idx = filename.lastIndexOf('.');def fname = filename.substring(0,idx);def file2 = fname +".xlsx" as String;def shname = fname.tokenize('\\')[-1] as String;println "${file2} , ${shname}"// 建立新的Excel 活頁簿  XSSFWorkbook workbook = new XSSFWorkbook();XSSFSheet sheet = workbook.createSheet(shname);// 用于格式化儲存格的資料  XSSFDataFormat format = workbook.createDataFormat();// 設定儲存格類型  XSSFCellStyle cellStyle = workbook.createCellStyle();cellStyle.setDataFormat(format.getFormat("0.00;-0.00"));XSSFRow row = null;XSSFCell cell = null;int n=0;int i=0; // rowIndexint j=0;def alist =[]new File(filename).eachLine{ line ->alist = line.tokenize('\t');n = alist.size();  if (n >0){row = sheet.createRow(i); // 建立新行(row)for(j=0; j<n; j++){cell = row.createCell(j); // 建立儲存格if (alist[j]!=null)if (alist[j]=~/^-?[0-9]+(.[0-9]+)?$/){ // 是數字cell.setCellValue(new BigDecimal(alist[j]));cell.setCellStyle(cellStyle);} else {cell.setCellValue((String)alist[j]);}}i ++ ;}}// 建立檔案輸出資料流,輸出試算表try {def fout = new FileOutputStream(file2);workbook.write(fout);fout.close();} catch(e){println e;}}static void main(args) {def filename =null;if (args.size()==1)filename = getFilename(args[0]);elsefilename = getFilename('.');if (filename==null) return;def app = new Text2xlsx();app.write_xlsx(filename);}}

直接運行 groovy write_xlsx

參考 poi-3.10-FINAL/docs/spreadsheet/quick-guide.html


java用POI 匯出的Excel表開啟時提示“xxxxxlsx 發現不可讀取的內容 是否恢複”

不是軟體的問題,是你程式的問題吧
 
急java用poi匯出excel檔案,開啟匯出的檔案時報錯“檔案錯誤,資料可可以丟失”

我遇到過這個問題後調查發現兩個原因:
1.你的excel模版本身有問題,可以嘗試建立一個模版。
2.你的excel使用了一些POI不支援的函數。
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.