1. 1.创建工作簿 (WORKBOOK)
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  4.  
  5. wb.write(fileOut);
  6.  
  7. fileOut.close();
  1. 2.创建工作表(SHEET
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet1 = wb.createSheet("new sheet");
  4.  
  5. HSSFSheet sheet2 = wb.createSheet("second sheet");
  6.  
  7. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  8.  
  9. wb.write(fileOut);
  10.  
  11. fileOut.close();
  1. 3.创建单元格(CELL)
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("new sheet");
  4.  
  5. // Create a row and put some cells in it. Rows are 0 based.
  6.  
  7. HSSFRow row = sheet.createRow((short)0);
  8.  
  9. // Create a cell and put a value in it.
  10.  
  11. HSSFCell cell = row.createCell((short)0);
  12.  
  13. cell.setCellValue(1);
  14.  
  15. // Or do it on one line.
  16.  
  17. row.createCell((short)1).setCellValue(1.2);
  18.  
  19. row.createCell((short)2).setCellValue("This is a string");
  20.  
  21. row.createCell((short)3).setCellValue(true);
  22.  
  23. // Write the output to a file
  24.  
  25. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  26.  
  27. wb.write(fileOut);
  28.  
  29. fileOut.close();
  1. 4.创建指定单元格式的单元格
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("new sheet");
  4.  
  5. // Create a row and put some cells in it. Rows are 0 based.
  6.  
  7. HSSFRow row = sheet.createRow((short)0);
  8.  
  9. // Create a cell and put a date value in it. The first cell is not styled
  10.  
  11. // as a date.
  12.  
  13. HSSFCell cell = row.createCell((short)0);
  14.  
  15. cell.setCellValue(new Date());
  16.  
  17. // we style the second cell as a date (and time). It is important to
  18.  
  19. // create a new cell style from the workbook otherwise you can end up
  20.  
  21. // modifying the built in style and effecting not only this cell but other cells.
  22.  
  23. HSSFCellStyle cellStyle = wb.createCellStyle();
  24.  
  25. cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
  26.  
  27. cell = row.createCell((short)1);
  28.  
  29. cell.setCellValue(new Date());
  30.  
  31. cell.setCellStyle(cellStyle);
  32.  
  33. // Write the output to a file
  34.  
  35. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  36.  
  37. wb.write(fileOut);
  38.  
  39. fileOut.close();
  1. 5. 单元格的不同格式
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("new sheet");
  4.  
  5. HSSFRow row = sheet.createRow((short)2);
  6.  
  7. row.createCell((short) 0).setCellValue(1.1);
  8.  
  9. row.createCell((short) 1).setCellValue(new Date());
  10.  
  11. row.createCell((short) 2).setCellValue("a string");
  12.  
  13. row.createCell((short) 3).setCellValue(true);
  14.  
  15. row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
  16.  
  17. // Write the output to a file
  18.  
  19. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  20.  
  21. wb.write(fileOut);
  22.  
  23. fileOut.close();
  1. 6.单元格的不通对齐方式
  1. public static void main(String[] args)
  2.  
  3. throws IOException
  4.  
  5. {
  6.  
  7. HSSFWorkbook wb = new HSSFWorkbook();
  8.  
  9. HSSFSheet sheet = wb.createSheet("new sheet");
  10.  
  11. HSSFRow row = sheet.createRow((short) 2);
  12.  
  13. createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
  14.  
  15. createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
  16.  
  17. createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
  18.  
  19. createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
  20.  
  21. createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
  22.  
  23. createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
  24.  
  25. createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
  26.  
  27. // Write the output to a file
  28.  
  29. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  30.  
  31. wb.write(fileOut);
  32.  
  33. fileOut.close();
  34.  
  35. }
  36.  
  37. /**
  38.  
  39. * Creates a cell and aligns it a certain way.
  40.  
  41. *
  42.  
  43. * @param wb the workbook
  44.  
  45. * @param row the row to create the cell in
  46.  
  47. * @param column the column number to create the cell in
  48.  
  49. * @param align the alignment for the cell.
  50.  
  51. */
  52.  
  53. private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
  54.  
  55. {
  56.  
  57. HSSFCell cell = row.createCell(column);
  58.  
  59. cell.setCellValue("Align It");
  60.  
  61. HSSFCellStyle cellStyle = wb.createCellStyle();
  62.  
  63. cellStyle.setAlignment(align);
  64.  
  65. cell.setCellStyle(cellStyle);
  66.  
  67. }
  1. 7.单元格的边框设置
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("new sheet");
  4.  
  5. // Create a row and put some cells in it. Rows are 0 based.
  6.  
  7. HSSFRow row = sheet.createRow((short) 1);
  8.  
  9. // Create a cell and put a value in it.
  10.  
  11. HSSFCell cell = row.createCell((short) 1);
  12.  
  13. cell.setCellValue(4);
  14.  
  15. // Style the cell with borders all around.
  16.  
  17. HSSFCellStyle style = wb.createCellStyle();
  18.  
  19. style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  20.  
  21. style.setBottomBorderColor(HSSFColor.BLACK.index);
  22.  
  23. style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  24.  
  25. style.setLeftBorderColor(HSSFColor.GREEN.index);
  26.  
  27. style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  28.  
  29. style.setRightBorderColor(HSSFColor.BLUE.index);
  30.  
  31. style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
  32.  
  33. style.setTopBorderColor(HSSFColor.BLACK.index);
  34.  
  35. cell.setCellStyle(style);
  36.  
  37. // Write the output to a file
  38.  
  39. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  40.  
  41. wb.write(fileOut);
  42.  
  43. fileOut.close();
  1. 8.填充和颜色设置
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("new sheet");
  4.  
  5. // Create a row and put some cells in it. Rows are 0 based.
  6.  
  7. HSSFRow row = sheet.createRow((short) 1);
  8.  
  9. // Aqua background
  10.  
  11. HSSFCellStyle style = wb.createCellStyle();
  12.  
  13. style.setFillBackgroundColor(HSSFColor.AQUA.index);
  14.  
  15. style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
  16.  
  17. HSSFCell cell = row.createCell((short) 1);
  18.  
  19. cell.setCellValue("X");
  20.  
  21. cell.setCellStyle(style);
  22.  
  23. // Orange "foreground", foreground being the fill foreground not the font color.
  24.  
  25. style = wb.createCellStyle();
  26.  
  27. style.setFillForegroundColor(HSSFColor.ORANGE.index);
  28.  
  29. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  30.  
  31. cell = row.createCell((short) 2);
  32.  
  33. cell.setCellValue("X");
  34.  
  35. cell.setCellStyle(style);
  36.  
  37. // Write the output to a file
  38.  
  39. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  40.  
  41. wb.write(fileOut);
  42.  
  43. fileOut.close();
  1. 9.合并单元格操作
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("new sheet");
  4.  
  5. HSSFRow row = sheet.createRow((short) 1);
  6.  
  7. HSSFCell cell = row.createCell((short) 1);
  8.  
  9. cell.setCellValue("This is a test of merging");
  10.  
  11. sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
  12.  
  13. // Write the output to a file
  14.  
  15. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  16.  
  17. wb.write(fileOut);
  18.  
  19. fileOut.close();
  1. 10.字体设置
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("new sheet");
  4.  
  5. // Create a row and put some cells in it. Rows are 0 based.
  6.  
  7. HSSFRow row = sheet.createRow((short) 1);
  8.  
  9. // Create a new font and alter it.
  10.  
  11. HSSFFont font = wb.createFont();
  12.  
  13. font.setFontHeightInPoints((short)24);
  14.  
  15. font.setFontName("Courier New");
  16.  
  17. font.setItalic(true);
  18.  
  19. font.setStrikeout(true);
  20.  
  21. // Fonts are set into a style so create a new one to use.
  22.  
  23. HSSFCellStyle style = wb.createCellStyle();
  24.  
  25. style.setFont(font);
  26.  
  27. // Create a cell and put a value in it.
  28.  
  29. HSSFCell cell = row.createCell((short) 1);
  30.  
  31. cell.setCellValue("This is a test of fonts");
  32.  
  33. cell.setCellStyle(style);
  34.  
  35. // Write the output to a file
  36.  
  37. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  38.  
  39. wb.write(fileOut);
  40.  
  41. fileOut.close();
  1. 11.自定义颜色
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet();
  4.  
  5. HSSFRow row = sheet.createRow((short) 0);
  6.  
  7. HSSFCell cell = row.createCell((short) 0);
  8.  
  9. cell.setCellValue("Default Palette");
  10.  
  11. //apply some colors from the standard palette,
  12.  
  13. // as in the previous examples.
  14.  
  15. //we'll use red text on a lime background
  16.  
  17. HSSFCellStyle style = wb.createCellStyle();
  18.  
  19. style.setFillForegroundColor(HSSFColor.LIME.index);
  20.  
  21. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  22.  
  23. HSSFFont font = wb.createFont();
  24.  
  25. font.setColor(HSSFColor.RED.index);
  26.  
  27. style.setFont(font);
  28.  
  29. cell.setCellStyle(style);
  30.  
  31. //save with the default palette
  32.  
  33. FileOutputStream out = new FileOutputStream("default_palette.xls");
  34.  
  35. wb.write(out);
  36.  
  37. out.close();
  38.  
  39. //now, let's replace RED and LIME in the palette
  40.  
  41. // with a more attractive combination
  42.  
  43. // (lovingly borrowed from freebsd.org)
  44.  
  45. cell.setCellValue("Modified Palette");
  46.  
  47. //creating a custom palette for the workbook
  48.  
  49. HSSFPalette palette = wb.getCustomPalette();
  50.  
  51. //replacing the standard red with freebsd.org red
  52.  
  53. palette.setColorAtIndex(HSSFColor.RED.index,
  54.  
  55. (byte) 153, //RGB red (0-255)
  56.  
  57. (byte) 0, //RGB green
  58.  
  59. (byte) 0 //RGB blue
  60.  
  61. );
  62.  
  63. //replacing lime with freebsd.org gold
  64.  
  65. palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
  66.  
  67. //save with the modified palette
  68.  
  69. // note that wherever we have previously used RED or LIME, the
  70.  
  71. // new colors magically appear
  72.  
  73. out = new FileOutputStream("modified_palette.xls");
  74.  
  75. wb.write(out);
  76.  
  77. out.close();
  1. 12.读和重写EXCEL文件
  1. POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("workbook.xls"));
  2.  
  3. HSSFWorkbook wb = new HSSFWorkbook(fs);
  4.  
  5. HSSFSheet sheet = wb.getSheetAt(0);
  6.  
  7. HSSFRow row = sheet.getRow(2);
  8.  
  9. HSSFCell cell = row.getCell((short)3);
  10.  
  11. if (cell == null)
  12.  
  13. cell = row.createCell((short)3);
  14.  
  15. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  16.  
  17. cell.setCellValue("a test");
  18.  
  19. // Write the output to a file
  20.  
  21. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  22.  
  23. wb.write(fileOut);
  24.  
  25. fileOut.close();
  1. 13.EXCEL单元格中使用自动换行
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet s = wb.createSheet();
  4.  
  5. HSSFRow r = null;
  6.  
  7. HSSFCell c = null;
  8.  
  9. HSSFCellStyle cs = wb.createCellStyle();
  10.  
  11. HSSFFont f = wb.createFont();
  12.  
  13. HSSFFont f2 = wb.createFont();
  14.  
  15. cs = wb.createCellStyle();
  16.  
  17. cs.setFont( f2 );
  18.  
  19. //Word Wrap MUST be turned on
  20.  
  21. cs.setWrapText( true );
  22.  
  23. r = s.createRow( (short) 2 );
  24.  
  25. r.setHeight( (short) 0x349 );
  26.  
  27. c = r.createCell( (short) 2 );
  28.  
  29. c.setCellType( HSSFCell.CELL_TYPE_STRING );
  30.  
  31. c.setCellValue( "Use /n with word wrap on to create a new line" );
  32.  
  33. c.setCellStyle( cs );
  34.  
  35. s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
  36.  
  37. FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
  38.  
  39. wb.write( fileOut );
  40.  
  41. fileOut.close();
  1. 14.数字格式自定义
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("format sheet");
  4.  
  5. HSSFCellStyle style;
  6.  
  7. HSSFDataFormat format = wb.createDataFormat();
  8.  
  9. HSSFRow row;
  10.  
  11. HSSFCell cell;
  12.  
  13. short rowNum = 0;
  14.  
  15. short colNum = 0;
  16.  
  17. row = sheet.createRow(rowNum++);
  18.  
  19. cell = row.createCell(colNum);
  20.  
  21. cell.setCellValue(11111.25);
  22.  
  23. style = wb.createCellStyle();
  24.  
  25. style.setDataFormat(format.getFormat("0.0"));
  26.  
  27. cell.setCellStyle(style);
  28.  
  29. row = sheet.createRow(rowNum++);
  30.  
  31. cell = row.createCell(colNum);
  32.  
  33. cell.setCellValue(11111.25);
  34.  
  35. style = wb.createCellStyle();
  36.  
  37. style.setDataFormat(format.getFormat("#,##0.0000"));
  38.  
  39. cell.setCellStyle(style);
  40.  
  41. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  42.  
  43. wb.write(fileOut);
  44.  
  45. fileOut.close();
  1. 15.调整工作单位置
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("format sheet");
  4.  
  5. HSSFPrintSetup ps = sheet.getPrintSetup();
  6.  
  7. sheet.setAutobreaks(true);
  8.  
  9. ps.setFitHeight((short)1);
  10.  
  11. ps.setFitWidth((short)1);
  12.  
  13. // Create various cells and rows for spreadsheet.
  14.  
  15. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  16.  
  17. wb.write(fileOut);
  18.  
  19. fileOut.close();
  1. 16.设置打印区域
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("Sheet1");
  4.  
  5. wb.setPrintArea(0, "$A$1:$C$2");
  6.  
  7. //sets the print area for the first sheet
  8.  
  9. //Alternatively:
  10.  
  11. //wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)
  12.  
  13. // Create various cells and rows for spreadsheet.
  14.  
  15. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  16.  
  17. wb.write(fileOut);
  18.  
  19. fileOut.close();
  1. 17.标注脚注
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("format sheet");
  4.  
  5. HSSFFooter footer = sheet.getFooter()
  6.  
  7. footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
  8.  
  9. // Create various cells and rows for spreadsheet.
  10.  
  11. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  12.  
  13. wb.write(fileOut);
  14.  
  15. fileOut.close();
  1. 18.使用方便的内部提供的函数
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet1 = wb.createSheet( "new sheet" );
  4.  
  5. // Create a merged region
  6.  
  7. HSSFRow row = sheet1.createRow( (short) 1 );
  8.  
  9. HSSFRow row2 = sheet1.createRow( (short) 2 );
  10.  
  11. HSSFCell cell = row.createCell( (short) 1 );
  12.  
  13. cell.setCellValue( "This is a test of merging" );
  14.  
  15. Region region = new Region( 1, (short) 1, 4, (short) 4 );
  16.  
  17. sheet1.addMergedRegion( region );
  18.  
  19. // Set the border and border colors.
  20.  
  21. final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
  22.  
  23. HSSFRegionUtil.setBorderBottom( borderMediumDashed,
  24.  
  25. region, sheet1, wb );
  26.  
  27. HSSFRegionUtil.setBorderTop( borderMediumDashed,
  28.  
  29. region, sheet1, wb );
  30.  
  31. HSSFRegionUtil.setBorderLeft( borderMediumDashed,
  32.  
  33. region, sheet1, wb );
  34.  
  35. HSSFRegionUtil.setBorderRight( borderMediumDashed,
  36.  
  37. region, sheet1, wb );
  38.  
  39. HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
  40.  
  41. HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
  42.  
  43. HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
  44.  
  45. HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
  46.  
  47. // Shows some usages of HSSFCellUtil
  48.  
  49. HSSFCellStyle style = wb.createCellStyle();
  50.  
  51. style.setIndention((short)4);
  52.  
  53. HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
  54.  
  55. HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
  56.  
  57. HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);
  58.  
  59. // Write out the workbook
  60.  
  61. FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
  62.  
  63. wb.write( fileOut );
  64.  
  65. fileOut.close();
  1. 19.在工作单中移动行,调整行的上下位置
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("row sheet");
  4.  
  5. // Create various cells and rows for spreadsheet.
  6.  
  7. // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
  8.  
  9. sheet.shiftRows(5, 10, -5);
  10.  
  11. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  12.  
  13. wb.write(fileOut);
  14.  
  15. fileOut.close();
  1. 20.选种指定的工作单
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("row sheet");
  4.  
  5. sheet.setSelected(true);
  6.  
  7. // Create various cells and rows for spreadsheet.
  8.  
  9. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  10.  
  11. wb.write(fileOut);
  12.  
  13. fileOut.close();
  1. 21.工作单的放大缩小
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet1 = wb.createSheet("new sheet");
  4.  
  5. sheet1.setZoom(3,4); // 75 percent magnification
  6.  
  7. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  8.  
  9. wb.write(fileOut);
  10.  
  11. fileOut.close();
  1. 22.头注和脚注
  1. HSSFWorkbook wb = new HSSFWorkbook();
  2.  
  3. HSSFSheet sheet = wb.createSheet("new sheet");
  4.  
  5. HSSFHeader header = sheet.getHeader();
  6.  
  7. header.setCenter("Center Header");
  8.  
  9. header.setLeft("Left Header");
  10.  
  11. header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
  12.  
  13. HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
  14.  
  15. FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  16.  
  17. wb.write(fileOut);
  18.  
  19. fileOut.close();

三、重点介绍

一个excel文档必备的内容都有什么呢:

1、文档的名字和保存的位置。(文档以xls结尾)

2、sheet页,一个excel文档可以包括多个sheet页

3、行

4、单元格(有些单元格是合并的)

5、单元格样式(包括背景颜色、对其方式等)

  1. HSSFWorkbook wb = new HSSFWorkbook(); //创建excel
  2.  
  3. HSSFSheet sheet1 = wb.createSheet("new sheet"); //创建多个sheet页
  4. HSSFSheet sheet2 = wb.createSheet("second sheet");
  5.  
  6. HSSFCellStyle style = wb.createCellStyle(); //设置单元格背景颜色
  7. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  8. style.setFillForegroundColor(HSSFColor.YELLOW.index);
  9.  
  10. HSSFRow row = sheet1.createRow((short)0);//创建一行数据,
  11.  
  12. row.createCell((short)0).setCellValue(1); //给第一行数据赋值
  13. row.createCell((short)1).setCellValue(1.2);
  14. row.createCell((short)2).setCellValue("This is a string");
  15. HSSFCell cell = row.createCell((short)3);
  16. cell.setCellValue(true);
  17. cell.setCellStyle(style);//设置单元格背景颜色
  18.  
  19. sheet1.addMergedRegion(new Region(0,(short)1,1,(short)1)); //合并单元格
  20.  
  21. HSSFCellStyle style_green = wb.createCellStyle(); //设置单元格背景颜色
  22. style_green.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  23. style_green.setFillForegroundColor(HSSFColor.LIME.index);
  24. style_green.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
  25. style_green.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
  26. style_green.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
  27. style_green.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
  28. style_green.setWrapText( true );//自动回车
  29. style_green.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//设置竖直方向居中
  30. style_green.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置水平方向居中
  31.  
  32. sheet.setColumnWidth( (short) k, (short) ( ( 50 * 4 ) / ( (double) 1 / 20 ) ) );//设置单元格宽度
  33.  
  34. row1.setHeight((short) 0x220 );//设置行高度

页面弹出保存位置:

  1. req.setCharacterEncoding("UTF-8");
  2. resp.setCharacterEncoding("UTF-8");
  3. resp.setContentType("application/x-download");
  4. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
  5. String filedisplay = sdf.format(new Date())+".xls";
  6. filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
  7. resp.addHeader("Content-Disposition", "attachment;filename="+ filedisplay);
  8. OutputStream out = resp.getOutputStream();
  9. wb.write(out);

背景颜色:

  1. HSSFColor.ROYAL_BLUE
  2. HSSFColor.TEAL
  3. HSSFColor.LIME
  4. HSSFColor.PALE_BLUE
  5. HSSFColor.AQUA
  6. HSSFColor.GREEN
  7. HSSFColor.TURQUOISE
  8. HSSFColor.DARK_BLUE
  9. HSSFColor.CORNFLOWER_BLUE
  10. HSSFColor.OLIVE_GREEN
  11. HSSFColor.WHITE
  12. HSSFColor.LIGHT_TURQUOISE
  13. HSSFColor.LEMON_CHIFFON
  14. HSSFColor.LIGHT_GREEN
  15. HSSFColor.BLUE
  16. HSSFColor.DARK_RED
  17. HSSFColor.CORAL
  18. HSSFColor.RED
  19. HSSFColor.LIGHT_YELLOW
  20. HSSFColor.SKY_BLUE
  21. HSSFColor.BROWN
  22. HSSFColor.SEA_GREEN
  23. HSSFColor.INDIGO
  24. HSSFColor.MAROON
  25. HSSFColor.GREY_80_PERCENT
  26. HSSFColor.GREY_25_PERCENT
  27. HSSFColor.DARK_GREEN
  28. HSSFColor.YELLOW
  29. HSSFColor.GOLD
  30. HSSFColor.GREY_40_PERCENT
  31. HSSFColor.DARK_TEAL
  32. HSSFColor.PINK
  33. HSSFColor.ORCHID
  34. HSSFColor.LIGHT_BLUE
  35. HSSFColor.LIGHT_CORNFLOWER_BLUE
  36. HSSFColor.BLACK
  37. HSSFColor.DARK_YELLOW
  38. HSSFColor.VIOLET
  39. HSSFColor.LAVENDER
  40. HSSFColor.ROSE
  41. HSSFColor.BLUE_GREY
  42. HSSFColor.LIGHT_ORANGE
  43. HSSFColor.ORANGE
  44. HSSFColor.GREY_50_PERCENT

啦啦啦


  1. 啦啦啦

Java -- POI -- 入门使用以及简单介绍的更多相关文章

  1. 【浅墨著作】《OpenCV3编程入门》内容简单介绍&勘误&配套源码下载

    经过近一年的沉淀和总结,<OpenCV3编程入门>一书最终和大家见面了. 近期有为数不少的小伙伴们发邮件给浅墨建议最好在博客里面贴出这本书的文件夹,方便大家更好的了解这本书的内容.事实上近 ...

  2. Java EE设计模式(主要简单介绍工厂模式,适配器模式和模板方法模式)

    Java EE设计模式分为三种类型,共23种: 创建型模式:单例模式.抽象工厂模式.建造者模式.工厂模式.原型模式. 结构型模式:适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式 ...

  3. webpack入门篇--1.简单介绍

    简单介绍: webpack是一个模块打包工具,给js准备的打包工具,可以把很多的模块打包成很少的文件 目标: 1.切分依赖数,分到不同代码块里,按需加载,懒加 载 2.任何静态资源都可以被视为一个模块 ...

  4. 漫游Kafka入门篇之简单介绍

    介绍 Kafka是一个分布式的.可分区的.可复制的消息系统.它提供了普通消息系统的功能,但具有自己独特的设计.这个独特的设计是什么样的呢?   首先让我们看几个基本的消息系统术语: Kafka将消息以 ...

  5. 漫游Kafka入门篇之简单介绍(1)

    介绍 Kafka是一个分布式的.可分区的.可复制的消息系统.它提供了普通消息系统的功能,但具有自己独特的设计.这个独特的设计是什么样的呢?   首先让我们看几个基本的消息系统术语: Kafka将消息以 ...

  6. Java静态检测工具/Java代码规范和质量检查简单介绍(转)

    静态检查: 静态测试包括代码检查.静态结构分析.代码质量度量等.它可以由人工进行,充分发挥人的逻辑思维优势,也可以借助软件工具自动进行.代码检查代码检查包括代码走查.桌面检查.代码审查等,主要检查代码 ...

  7. (转)漫游Kafka入门篇之简单介绍

    转自:http://blog.csdn.net/honglei915/article/details/37564521 原文地址:http://blog.csdn.net/honglei915/art ...

  8. Java并发:ThreadLocal的简单介绍

    作者:汤圆 个人博客:javalover.cc 前言 前面在线程的安全性中介绍过全局变量(成员变量)和局部变量(方法或代码块内的变量),前者在多线程中是不安全的,需要加锁等机制来确保安全,后者是线程安 ...

  9. 【细说Java】方法重载的简单介绍

    1. 什么是重载 方法名称相同,但它们的参数类型或个数不同,这样,方法在被调用时编译器就可以根据参数的类型与个数的不同加以区分,这就是方法的重载. 既然可以通过参数类型或参数个数来作为重载条件,那返回 ...

随机推荐

  1. unity中尽量规避的C#写法

    首先想到的也就是重名的问题. 创建C#脚本的时候,引擎会搜索所有的文件夹里已有的C#脚本,若已经存在这个名字的脚本文件,那么系统就会报错:    再者,类名和方法名不能相同,理解来就是,方法名和类名相 ...

  2. 域名DNS解析工具ping/nslookup/dig/host

    常见 DNS 记录的类型 类型 目的 A 地址记录,用来指定域名的 IPv4 地址,如果需要将域名指向一个 IP 地址,就需要添加 A 记录. AAAA 用来指定主机名(或域名)对应的 IPv6 地址 ...

  3. ITxlab倡议启动“互联网X大脑”计划

    导语:"互联网X大脑"计划由ITxlab(互联网X实验室)联合科学院相关机构.基于7年以来取得的研究成果,倡议建立的互联网与脑科学前沿研究平台,吸引不同领域专家进行科学研究和成果交 ...

  4. 【BZOJ3036】绿豆蛙的归宿 概率DP

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  5. C#编程(八十二)---------- 用户自定义异常类

    用户自定义异常类 前面已经说了不少关于异常的问题了,现在来给大家说一下自定义异常时咋个回事以及咋样. 为啥会出现自定义异常类呢?用用脚趾头想想也明白,是为了定义咱们自己的异常,自定义异常类继承自App ...

  6. Deploying JAR Package & JSP Page in EBS R12.2.4 WLS

    https://pan.baidu.com/s/1OomyeLdbGWxTtCKVcweo0w # Uninstall JAR JSP QRCODE 1.# 查找QRCODE相关文件位置 [root@ ...

  7. Drawing line on a click on ZedGraph Pane

    https://stackoverflow.com/questions/12422398/drawing-line-on-a-click-on-zedgraph-pane public Form1() ...

  8. CSS网页布局垂直居中整理

    一.使用CSS3处理垂直居中方式 1.使用Flex布局处理(推荐),简单好用 body,html{ width:100%; height:100%; } .out { width: 20%; heig ...

  9. 奇怪吸引子---Qi

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...

  10. android-activity生命周期方法

    整个Activity生命周期中的所有方法,我们可以根据程序的需要来覆盖相应的方法: public class Activity extends ApplicationContext { //创建的时候 ...