package poi_test; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFPrintSetup; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelTest { public static void main(String[] args) throws IOException { //新建文件輸出流對象 FileOutputStream out = new FileOutputStream("F:/poitest.xls"); //新建workbook HSSFWorkbook wb = new HSSFWorkbook(); //新建sheet HSSFSheet sheet = wb.createSheet(); //新建行 HSSFRow row = sheet.createRow(2); //設置行高 row.setHeightInPoints(20); //新建單元格 HSSFCell cell = row.createCell(2); //創建整個文本的字體對象,workbook創建 HSSFFont cnFont = wb.createFont(); //設置字體行高,字體名字 cnFont.setFontHeightInPoints((short)10); cnFont.setFontName("隸書"); //將文本字面格式用到單元格上,新建單元格風格 HSSFCellStyle cnStyle = wb.createCellStyle(); cnStyle.setFont(cnFont); cell.setCellStyle(cnStyle); //單元格內文本對象新建,HSSFRichTextString的應用 HSSFRichTextString richText = new HSSFRichTextString("中文字體測試"); cell.setCellValue(richText); //再建一個單元格,重復上面的設置 HSSFCell enCell = row.createCell(3); HSSFFont enFont = wb.createFont(); enFont.setFontHeightInPoints((short) 10); enFont.setFontName("Arial Black"); HSSFCellStyle enStyle = wb.createCellStyle(); enStyle.setFont(enFont); enCell.setCellStyle(enStyle); enCell.setCellValue(new HSSFRichTextString("English font test")); sheet.setColumnWidth(2, 4000); sheet.setColumnWidth(3, 4000); //輸出 //設置邊框 sheet.setDisplayGridlines(false); //設置打印的邊框 sheet.setPrintGridlines(false); //設置打印對象 HSSFPrintSetup printSetup = sheet.getPrintSetup(); //設置頁邊距 printSetup.setHeaderMargin((double) 0.44); // 頁眉 printSetup.setFooterMargin((double) 0.2);//頁腳 //設置頁寬 printSetup.setFitWidth((short)1); printSetup.setFitHeight((short)1000); //設置打印方向,橫向就是true printSetup.setLandscape(true); //設置A4紙 printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //打印,關閉流 wb.write(out); out.close(); System.out.println("ok"); } }
//以下為轉載
HSSFSheet fromsheet = wb.getSheetAt(0); //模版頁 for(int num=0;num<addSheetNum;num++)//新增 { String numStr = String.valueOf(num+2); HSSFSheet newsheet = wb.createSheet("第"+numStr+"頁"); //設置打印參數 newsheet.setMargin(HSSFSheet.TopMargin,fromsheet.getMargin(HSSFSheet.TopMargin));// 頁邊距(上) newsheet.setMargin(HSSFSheet.BottomMargin,fromsheet.getMargin(HSSFSheet.BottomMargin));// 頁邊距(下) newsheet.setMargin(HSSFSheet.LeftMargin,fromsheet.getMargin(HSSFSheet.LeftMargin) );// 頁邊距(左) newsheet.setMargin(HSSFSheet.RightMargin,fromsheet.getMargin(HSSFSheet.RightMargin));// 頁邊距(右 HSSFPrintSetup ps = newsheet.getPrintSetup(); ps.setLandscape(false); // 打印方向,true:橫向,false:縱向(默認) ps.setVResolution((short)600); ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //紙張類型 SheetFunc.copyRows(wb, 0, num+1,0 , 46, 0);//復制 wb.getSheetAt(num+1).setColumnWidth((short)0, (short)2400);//256,31.38 }