- 测试的代码
package com.poi;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XSSFWriter {
public static void main(String[] args) throws Exception{
writeData();
// readData();
}
/**
* 解析输入数据的类型
* @param workbook
* @param fis
*/
public static void parseInput(XSSFWorkbook workbook, InputStream fis){
try {
if( !fis.markSupported() ){
fis=(InputStream)new PushbackInputStream(fis, 8);
}
if( POIXMLDocument.hasOOXMLHeader(fis) ){
workbook=new XSSFWorkbook(OPCPackage.open(fis));
}
if( POIFSFileSystem.hasPOIFSHeader(fis) ){
System.out.println(“—–”);
}
System.out.println(“—–++++”);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读数据
* @throws Exception
*/
public static void readData() throws Exception{
// InputStream fis=new FileInputStream(“E://xssfCreateExcel.xlsx”);
// Workbook workbook=new WorkbookFactory().create(fis);
// parseInput(workbook, fis);
// Iterator iterator = workboo;
// while( iterator.hasNext() ){//循环读取sheet
// XSSFSheet xssfSheet=iterator.next();
// Iterator iterator2 = xssfSheet.iterator();
// while( iterator2.hasNext() ){
// XSSFRow row=(XSSFRow)iterator2.next();
// Iterator iterator3 = row.iterator();
// while( iterator3.hasNext() ){
// Cell next = iterator3.next();
// switch (next.getCellType()) {
// case Cell.CELL_TYPE_NUMERIC:
// System.out.print(“NUMERIC– ” + next.getNumericCellValue());
// break;
// case Cell.CELL_TYPE_STRING:
// System.out.print(“STRING– ” + next.getStringCellValue());
// break;
// default:
// System.out.print(“RichString– ” + next.getRichStringCellValue());
// break;
// }
// }
// }
// }
}
/**
* POI 写数据到excel
* @throws Exception
*/
public static void writeData() throws Exception{
XSSFWorkbook workbook=new XSSFWorkbook();
FileOutputStream fos=new FileOutputStream(“E://xssfCreateExcel.xlsx”);
XSSFWriter.addData(workbook, “sheet1”);
XSSFWriter.addData(workbook, “sheet2”);
CreationHelper creationHelper=workbook.getCreationHelper();
XSSFHyperlink createHyperlink = (XSSFHyperlink)creationHelper.createHyperlink(XSSFHyperlink.LINK_DOCUMENT);
createHyperlink.setAddress(“”);
Cell cell=workbook.getSheet(“sheet1”).getRow(1).getCell(1);
// cell.setHyperlink(createHyperlink);
// cell.setCellStyle();
cell.setCellType(XSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula(“HYPERLINK(\”[xssfCreateExcel.xlsx]’sheet2’!Z10\”,\”链接到sheet2\”)”);
workbook.write(fos);
fos.close();
}
/**
* 向workbook的一个名为sheetName的sheet添加固定的数据
* @param workbook
* @param sheetName
*/
public static void addData(XSSFWorkbook workbook, String sheetName){
XSSFSheet createSheet = workbook.createSheet(sheetName);
Map