java操作excel (二)

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;

public class Excel {
         public static void main(String argv[]){ 
         try{
             HSSFWorkbook wb=new HSSFWorkbook();
             HSSFSheet sheet=wb.createSheet();
                
             //第一个样式和输出(边框)
             HSSFCellStyle style=wb.createCellStyle();
             style.setBorderBottom(HSSFCellStyle.BORDER_THIN);    //设置底线和颜色
             style.setBottomBorderColor(HSSFColor.BLACK.index);
             style.setBorderLeft(HSSFCellStyle.BORDER_THIN);   //设置左边线和颜色
             style.setLeftBorderColor(HSSFColor.BLACK.index);
             style.setBorderRight(HSSFCellStyle.BORDER_THIN);   //设置右边线和颜色
             style.setRightBorderColor(HSSFColor.BLACK.index);
             style.setBorderTop(HSSFCellStyle.BORDER_THIN);   //设置上面线和颜色
             style.setTopBorderColor(HSSFColor.BLACK.index);
             HSSFRow row=sheet.createRow((short)1);
             HSSFCell cell=row.createCell((short)1);
             cell.setCellValue("hello");                
             cell.setCellStyle(style);
            
            
             //第二个样式和输出(背景/前景色)
             style = wb.createCellStyle();
             style.setFillBackgroundColor(HSSFColor.ORANGE.index);    //添加背景色,内容看不清楚
             style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
             row=sheet.createRow((short)2);
             cell=row.createCell((short)2);
             cell.setCellValue("hello");
             cell.setCellStyle(style);
            
             style = wb.createCellStyle();
             style.setFillForegroundColor(HSSFColor.ORANGE.index);   //添加前景色,内容看的清楚
             style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
             row=sheet.createRow((short)3);
             cell=row.createCell((short)3);
             cell.setCellValue("hello");
             cell.setCellStyle(style);
            
            
             //第三个样式和输出(字体)
             HSSFFont font = wb.createFont();   //设置字体的样式
             font.setFontHeightInPoints((short)8);   //字体大小
             font.setFontName("Arial");   //什么字体
             font.setItalic(false);                 //是不倾斜
             font.setStrikeout(false);         //是不是划掉
             font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   //字体加粗
            
            
             style = wb.createCellStyle();   //引用字体的这个样式
             style.setFont(font);
            
             row=sheet.createRow((short)4);   //输出看结果
             cell=row.createCell((short)4);
             cell.setCellValue("BJ10012-1241");
             cell.setCellStyle(style);

            
             //第四样式和输出(自定义颜色)
             style = wb.createCellStyle();    //设置颜色样式,后面定义颜色的值
             style.setFillForegroundColor(HSSFColor.LIME.index);
             style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
             row=sheet.createRow((short)5);
             cell=row.createCell((short)5);
             cell.setCellValue("BJ10012-1241");
             cell.setCellStyle(style);
             HSSFPalette palette = wb.getCustomPalette();   //定义颜色的值
             palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 0, (byte) 0);

            
             //第五样式和输出(水平和垂直对齐)
             style = wb.createCellStyle(); 
             style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
             style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
             row=sheet.createRow((short)6);
             cell=row.createCell((short)6);
             cell.setCellValue("BJ10012-1241");
             cell.setCellStyle(style);
            
             //设置一个标题样式            
             style = wb.createCellStyle();
             font = wb.createFont();   //设置字体的样式
             font.setFontHeightInPoints((short)8);   //字体大小
             font.setFontName("Arial");   //什么字体
             font.setItalic(false);                 //是不倾斜
             font.setStrikeout(false);         //是不是划掉
             font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   //字体加粗
             style = wb.createCellStyle();   //引用字体的这个样式
             style.setFont(font);
             style.setBorderBottom(HSSFCellStyle.BORDER_THIN);    //设置底线和颜色
             style.setBottomBorderColor(HSSFColor.BLACK.index);
             style.setBorderLeft(HSSFCellStyle.BORDER_THIN);   //设置左边线和颜色
             style.setLeftBorderColor(HSSFColor.BLACK.index);
             style.setBorderRight(HSSFCellStyle.BORDER_THIN);   //设置右边线和颜色
             style.setRightBorderColor(HSSFColor.BLACK.index);
             style.setBorderTop(HSSFCellStyle.BORDER_THIN);   //设置上面线和颜色
             style.setTopBorderColor(HSSFColor.BLACK.index);
             style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
             style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
             style.setFillForegroundColor(HSSFColor.ORANGE.index);   //添加前景色,内容看的清楚
             style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            
             row=sheet.createRow((short)7);
             cell=row.createCell((short)7);
             cell.setCellValue("BJ10012-1241");
             cell.setCellStyle(style);
            

             //输出文件
             FileOutputStream fileOut = new FileOutputStream("workbook.xls");
             wb.write(fileOut);
             fileOut.close();
             System.out.println("ok");
         }catch(Exception e){
                 e.printStackTrace();
         }

}
}

 

你可能感兴趣的:(java,apache,Excel)