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();
         }

}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值