//by gisoracle poi2.5 import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.*; import org.apache.poi.hssf.usermodel.*; import java.io.FileOutputStream; public class ylexcel { public ylexcel() { } private static void createCellTitle(HSSFWorkbook wb, HSSFRow row, int column, String value) { HSSFCell cell = row.createCell( (short) column); cell.setEncoding(HSSFCell.ENCODING_UTF_16); HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont font = wb.createFont(); font.setFontHeightInPoints( (short) 11); ; font.setFontName("宋体"); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); cellStyle.setFont(font); cell.setCellStyle(cellStyle); cell.setCellValue(value); } private static void createsheet(String fileName) { //声明一个工作薄 HSSFWorkbook wb = new HSSFWorkbook(); //生成一个表格 HSSFSheet sheet = wb.createSheet(); //生成一个列 HSSFRow row = sheet.createRow(0); //生成一个样式 HSSFCellStyle style = wb.createCellStyle(); //设置这些样式 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //生成一个字体 HSSFFont font = wb.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints( (short) 16); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //把字体应用到当前的样式 style.setFont(font); //填充单元格 for (short i = 0; i < 5; i++) { //声明一个单元格 HSSFCell cell = row.createCell(i); //设置单元格的字符值 //HSSFRichTextString hSSFRich=new cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue("PPPPP" + i); //设置单元格的样式 cell.setCellStyle(style); } row = sheet.createRow(1); createCellTitle(wb, row, 0, "站三"); FileOutputStream fout; try { fout = new FileOutputStream(fileName); //输出到文件 wb.write(fout); fout.close(); } catch (Exception e) { System.out.println("!!BANG!! xlCreate() : " + e); } finally { } } private static void createFile(String outputFile) { try { // Create a New XL Document HSSFWorkbook wb = new HSSFWorkbook(); // Make a worksheet in the XL document created HSSFSheet sheet = wb.createSheet(); // Create row at index zero ( Top Row) HSSFRow row = sheet.createRow( (short) 0); for (int i = 0; i < 10; i++) { createCellTitle(wb, row, i, "gisoracle" + i); } // The Output file is where the xls will be created FileOutputStream fOut = new FileOutputStream(outputFile); // Write the XL sheet wb.write(fOut); fOut.flush(); // Done Deal.. fOut.close(); System.out.println("File Created .."); } catch (Exception e) { System.out.println("!!BANG!! xlCreate() : " + e); } } public static void ylb(String ym) { System.out.println("poi"); //createFile("c:/test.xls"); createsheet("c:/gisoracle.xls"); System.out.println("=============="); } }