poi使用

package helloworld;
import java.io.FileOutputStream;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
//写excel操作
public class Main
{
    public static void main(String[] args) 
    {
    FileOutputStream fos = null;
     HSSFWorkbook wb = null;
            try 
            {
                     fos = new FileOutputStream("d:\\temp\\test.xls");
                     wb = new HSSFWorkbook();
                    HSSFSheet sheet;
                    HSSFRow row;
                    HSSFCell cell;
                    HSSFCellStyle style = wb.createCellStyle();
                    HSSFCellStyle style2 = wb.createCellStyle();
                    HSSFCellStyle style3 = wb.createCellStyle();
                    HSSFFont font = wb.createFont();
                    HSSFFont font2 = wb.createFont();
                    // 目录栏样式
                    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                    // set cell border
                    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                    style.setBorderTop(HSSFCellStyle.BORDER_THIN);
                    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
                    style.setWrapText(true);
                    // 标头样式
                    style2.setFillBackgroundColor(HSSFColor.GREEN.index);
                    // 设定此样式的的背景颜色填充
                    style2.setFillPattern(HSSFCellStyle.FINE_DOTS);
                    style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                    // set cell border
                    style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                    style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                    style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
                    style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
                    // 主体内容样式
                    style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                    // set cell border
                    style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                    style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                    style3.setBorderTop(HSSFCellStyle.BORDER_THIN);
                    style3.setBorderRight(HSSFCellStyle.BORDER_THIN);
                    font.setColor(HSSFFont.COLOR_NORMAL);
                    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                    font2.setColor(HSSFFont.COLOR_RED);
                    font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                    font2.setFontHeightInPoints((short) 15);
                    style.setFont(font);
                    style2.setFont(font2);
                    sheet = wb.createSheet();
                    wb.setSheetName(0, "唯一码", HSSFCell.ENCODING_UTF_16);
                    sheet.setDefaultColumnWidth((short) 12);
                    // 第一行列表头
                    row = sheet.createRow(0);
                    cell = row.createCell((short) 0);
                    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                    cell.setCellValue("唯一码管理");
                    sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 11));
                    cell.setCellStyle(style2);
                    row = sheet.createRow(1);
                    cell = row.createCell((short) 0);
                    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                    cell.setCellValue("条形码");
                    cell.setCellStyle(style);
                    cell = row.createCell((short) 1);
                    cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                    cell.setCellValue("唯一码");
                    cell.setCellStyle(style);

                    String line;
                    int i =0;
                   while(i<10)
                    {                       
                            row = sheet.createRow(i + 2);
                            cell = row.createCell((short) 0);
                            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                            cell.setCellValue("123333");//会员号
                            cell.setCellStyle(style3);
                            cell = row.createCell((short) 1);
                            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                            cell.setCellValue("sdfsfsdfsf"); //手机号
                            cell.setCellStyle(style3);
                            i++;
                    }
                     wb.write(fos);
                     fos.close();
  } 
  catch (Exception e)
                {
   
  } finally 
  {
   
   }
   if (fos != null) 
   {
    try 
    {
     fos.close();
    } catch (Exception e) {
     
    }
   }
  }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值