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) {
     
    }
   }
  }

}

 

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