標籤:
工程需要引入 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的樣式在這裡面定義好 ,每產生一個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));//設定列寬,即模板中的列為多寬,則產生的excel的列也為多寬}book.setText(2,0,"2015年05月21日");for (int m = 9; m <= 34; m++) {//實際要顯示的xls要放值的開始行和結束行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, "本季度快報");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號mLeftTopCF.setFontBold(true);//粗體mLeftTopCF.setHorizontalAlignment(CellFormat.eHorizontalAlignmentLeft);//居左mLeftTopCF.setVerticalAlignment(CellFormat.eVerticalAlignmentCenter);//垂直置中//輸入資料的儲存格的格式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);//垂直置中}}
java使用f1j9swing來產生excel檔案