JAVA编码(20)——JAVA使用f1j9swing来生成excel文件
生活随笔
收集整理的這篇文章主要介紹了
JAVA编码(20)——JAVA使用f1j9swing来生成excel文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
工程中需要引入 f1j9swing.jar包
package sinosoft_xsy;import java.io.IOException;import com.f1j.ss.BookModelImpl; import com.f1j.ss.CellFormat; import com.f1j.ss.Constants; import com.f1j.ss.WriteParams; import com.f1j.util.F1Exception;public class Sinosoft {static CellFormat mCellFormat = new CellFormat();static CellFormat mLeftTopCF = new CellFormat();static CellFormat mInputCF = new CellFormat();public static void main(String[] args) {testExcel();}/*** 使用f1j9swing來生成excel*/public static void testExcel(){BookModelImpl book = new BookModelImpl();BookModelImpl modelBook = new BookModelImpl();try {book.initWorkbook();setCellFormat();//讀取已有的模板modelBook.read("E:\\workspace3.7\\gs_acc\\hkcwweblogic\\account\\yd_work\\BulletinReport\\BulletinReport_1.xls");//讀取模版Excel,所有cell的樣式在這里面定義好 ,每生成一個(gè)sheet就copy下,最省事book.copyRange(0, 0, modelBook.getLastRow(), modelBook.getLastCol(), modelBook, 0, 0, modelBook.getLastRow(), modelBook.getLastCol(), Constants.eCopyAll);for (int i = 0; i <= modelBook.getLastCol(); i++) {book.setColWidth(i, modelBook.getColWidth(i));//設(shè)置列寬,即模板中的列為多寬,則生成的excel的列也為多寬}book.setText(2,0,"2015年05月21日");for (int m = 9; m <= 34; m++) {//實(shí)際要顯示的xls要放值的開始行和結(jié)束行System.out.println(m);book.setNumber(m-1, 3, 11);book.setNumber(m-1, 4, 11);book.setNumber(m-1, 5, 11);} book.setSheetName(0, "本季度快報(bào)");book.insertSheets(0, 1);// Excel文件輸出book.write("E:\\workspace3.7\\gs_acc\\hkcwweblogic\\account\\yd_work\\BulletinReport\\BulletinReport_111111.xls", new WriteParams(BookModelImpl.eFileExcel97));// Excel文件輸出} catch (OutOfMemoryError e) {e.printStackTrace();} catch (F1Exception e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void setCellFormat() throws F1Exception{//生成單元格格式mCellFormat.setLeftBorder(CellFormat.eBorderThin);mCellFormat.setRightBorder(CellFormat.eBorderThin);mCellFormat.setTopBorder(CellFormat.eBorderThin);mCellFormat.setBottomBorder(CellFormat.eBorderThin);mCellFormat.setVerticalInsideBorder(CellFormat.eBorderThin);mCellFormat.setFontName("宋體");//左上角格式mLeftTopCF.setFontName("宋體");mLeftTopCF.setFontSize(200);//10號(hào)mLeftTopCF.setFontBold(true);//粗體mLeftTopCF.setHorizontalAlignment(CellFormat.eHorizontalAlignmentLeft);//居左mLeftTopCF.setVerticalAlignment(CellFormat.eVerticalAlignmentCenter);//垂直居中//輸入數(shù)據(jù)的單元格的格式mInputCF.setLeftBorder(CellFormat.eBorderThin);mInputCF.setRightBorder(CellFormat.eBorderThin);mInputCF.setTopBorder(CellFormat.eBorderThin);mInputCF.setBottomBorder(CellFormat.eBorderThin);mInputCF.setFontName("宋體");mInputCF.setFontSize(200);mInputCF.setHorizontalAlignment(CellFormat.eHorizontalAlignmentCenter);//居中mInputCF.setVerticalAlignment(CellFormat.eVerticalAlignmentCenter);//垂直居中} }
package sinosoft_xsy;import java.io.IOException;import com.f1j.ss.BookModelImpl; import com.f1j.ss.CellFormat; import com.f1j.ss.Constants; import com.f1j.ss.WriteParams; import com.f1j.util.F1Exception;public class Sinosoft {static CellFormat mCellFormat = new CellFormat();static CellFormat mLeftTopCF = new CellFormat();static CellFormat mInputCF = new CellFormat();public static void main(String[] args) {testExcel();}/*** 使用f1j9swing來生成excel*/public static void testExcel(){BookModelImpl book = new BookModelImpl();BookModelImpl modelBook = new BookModelImpl();try {book.initWorkbook();setCellFormat();//讀取已有的模板modelBook.read("E:\\workspace3.7\\gs_acc\\hkcwweblogic\\account\\yd_work\\BulletinReport\\BulletinReport_1.xls");//讀取模版Excel,所有cell的樣式在這里面定義好 ,每生成一個(gè)sheet就copy下,最省事book.copyRange(0, 0, modelBook.getLastRow(), modelBook.getLastCol(), modelBook, 0, 0, modelBook.getLastRow(), modelBook.getLastCol(), Constants.eCopyAll);for (int i = 0; i <= modelBook.getLastCol(); i++) {book.setColWidth(i, modelBook.getColWidth(i));//設(shè)置列寬,即模板中的列為多寬,則生成的excel的列也為多寬}book.setText(2,0,"2015年05月21日");for (int m = 9; m <= 34; m++) {//實(shí)際要顯示的xls要放值的開始行和結(jié)束行System.out.println(m);book.setNumber(m-1, 3, 11);book.setNumber(m-1, 4, 11);book.setNumber(m-1, 5, 11);} book.setSheetName(0, "本季度快報(bào)");book.insertSheets(0, 1);// Excel文件輸出book.write("E:\\workspace3.7\\gs_acc\\hkcwweblogic\\account\\yd_work\\BulletinReport\\BulletinReport_111111.xls", new WriteParams(BookModelImpl.eFileExcel97));// Excel文件輸出} catch (OutOfMemoryError e) {e.printStackTrace();} catch (F1Exception e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void setCellFormat() throws F1Exception{//生成單元格格式mCellFormat.setLeftBorder(CellFormat.eBorderThin);mCellFormat.setRightBorder(CellFormat.eBorderThin);mCellFormat.setTopBorder(CellFormat.eBorderThin);mCellFormat.setBottomBorder(CellFormat.eBorderThin);mCellFormat.setVerticalInsideBorder(CellFormat.eBorderThin);mCellFormat.setFontName("宋體");//左上角格式mLeftTopCF.setFontName("宋體");mLeftTopCF.setFontSize(200);//10號(hào)mLeftTopCF.setFontBold(true);//粗體mLeftTopCF.setHorizontalAlignment(CellFormat.eHorizontalAlignmentLeft);//居左mLeftTopCF.setVerticalAlignment(CellFormat.eVerticalAlignmentCenter);//垂直居中//輸入數(shù)據(jù)的單元格的格式mInputCF.setLeftBorder(CellFormat.eBorderThin);mInputCF.setRightBorder(CellFormat.eBorderThin);mInputCF.setTopBorder(CellFormat.eBorderThin);mInputCF.setBottomBorder(CellFormat.eBorderThin);mInputCF.setFontName("宋體");mInputCF.setFontSize(200);mInputCF.setHorizontalAlignment(CellFormat.eHorizontalAlignmentCenter);//居中mInputCF.setVerticalAlignment(CellFormat.eVerticalAlignmentCenter);//垂直居中} }
/*** 將生成的excel文件下載下來*/private static void outPutExcel() {FileInputStream fis = null;try {HttpServletResponse response = (HttpServletResponse) reqTL.get();response.reset();//避免下載excel出現(xiàn)亂碼情況response.setContentType("application/octet-stream;charset=gb2312");String filename=URLEncoder.encode("利潤快報(bào)(四)", "UTF-8")+"_1111111111.xls";response.setHeader("Content-disposition","attachment;filename="+filename);OutputStream outOS = response.getOutputStream();BufferedOutputStream bos = new BufferedOutputStream(outOS);fis = new FileInputStream("E:\\workspace3.7\\gs_acc\\hkcwweblogic\\account\\yd_work\\BulletinReport\\BulletinReport_111111.xls");byte[] buff = new byte[4096];int readLength = 0;while((readLength = fis.read(buff)) > 0){bos.write(buff, 0, readLength);}bos.flush();bos.close();} catch (Exception e) {e.printStackTrace();} finally{try{fis.close();java.io.File tmpfile = new java.io.File("E:\\workspace3.7\\gs_acc\\hkcwweblogic\\account\\yd_work\\BulletinReport\\BulletinReport_111111.xls");tmpfile.delete();}catch(IOException e1){e1.printStackTrace();}}}
轉(zhuǎn)載于:https://www.cnblogs.com/xushuyi/articles/4520552.html
總結(jié)
以上是生活随笔為你收集整理的JAVA编码(20)——JAVA使用f1j9swing来生成excel文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鸡啄米vc++2010系列40(文档、视
- 下一篇: 使用rapid-framework自动生