没有合适的资源?快使用搜索试试~ 我知道了~
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)。
资源推荐
资源详情
资源评论
How to create a new workbook
How to create a sheet
How to create cells
How to create date cells
Working with dierent types of cells
Iterate over rows and cells
Text Extraction
Aligning cells
Working with borders
Fills and color
Merging cells
Working with fonts
Custom colors
Reading and writing
Use newlines in cells.
Create user de&ned data formats
Fit Sheet to One Page
Set print area for a sheet
Set page numbers on the footer of a sheet
Shift rows
Set a sheet as selected
Set the zoom magni&cation for a sheet
Create split and freeze panes
Repeating rows and columns
Headers and Footers
Drawing Shapes
Styling Shapes
Shapes and Graphics2d
Outlining
Images
Named Ranges and Named Cells
How to set cell comments
How to adjust column width to &t the contents
Hyperlinks
Features
New Workbook
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new
FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
New Sheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new
FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Creating Cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0
based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a
string");
row.createCell((short)3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new
FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Creating Date Cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0
based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a date value in it. The
first cell is not styled
// as a date.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(new Date());
// we style the second cell as a date (and time). It
is important to
// create a new cell style from the workbook
otherwise you can end up
// modifying the built in style and effecting not
only this cell but other cells.
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("
m/d/yy h:mm"));
cell = row.createCell((short)1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
// Write the output to a file
FileOutputStream fileOut = new
FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Working with dierent types of cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short)2);
row.createCell((short) 0).setCellValue(1.1);
row.createCell((short) 1).setCellValue(new Date());
row.createCell((short) 2).setCellValue("a string");
row.createCell((short) 3).setCellValue(true);
row.createCell((short)
4).setCellType(HSSFCell.CELL_TYPE_ERROR);
// Write the output to a file
FileOutputStream fileOut = new
FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Demonstrates various alignment options
public static void main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short) 2);
createCell(wb, row, (short) 0,
HSSFCellStyle.ALIGN_CENTER);
createCell(wb, row, (short) 1,
HSSFCellStyle.ALIGN_CENTER_SELECTION);
createCell(wb, row, (short) 2,
HSSFCellStyle.ALIGN_FILL);
createCell(wb, row, (short) 3,
HSSFCellStyle.ALIGN_GENERAL);
createCell(wb, row, (short) 4,
HSSFCellStyle.ALIGN_JUSTIFY);
createCell(wb, row, (short) 5,
HSSFCellStyle.ALIGN_LEFT);
createCell(wb, row, (short) 6,
HSSFCellStyle.ALIGN_RIGHT);
// Write the output to a file
FileOutputStream fileOut = new
FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @param row the row to create the cell in
* @param column the column number to create the
cell in
* @param align the alignment for the cell.
*/
private static void createCell(HSSFWorkbook wb,
HSSFRow row, short column, short align)
{
HSSFCell cell = row.createCell(column);
cell.setCellValue("Align It");
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(align);
cell.setCellStyle(cellStyle);
}
Working with borders
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0
based.
HSSFRow row = sheet.createRow((short) 1);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue(4);
// Style the cell with borders all around.
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.GREEN.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLUE.index);
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
style.setTopBorderColor(HSSFColor.BLACK.index);
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new
FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
剩余43页未读,继续阅读
资源评论
sym65192973
- 粉丝: 0
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功