本文整理汇总了Java中org.apache.poi.hssf.usermodel.HSSFWorkbook类的典型用法代码示例。如果您正苦于以下问题:Java HSSFWorkbook类的具体用法?Java HSSFWorkbook怎么用?Java HSSFWorkbook使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HSSFWorkbook类属于org.apache.poi.hssf.usermodel包,在下文中一共展示了HSSFWorkbook类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readXls
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
/**
* Read the Excel 2003-2007
*
* @param path
* the path of the Excel
* @return
* @throws IOException
*/
public static String readXls(String path) throws IOException {
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
StringBuffer sb = new StringBuffer("");
// Read the Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// Read the Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
HSSFCell no = hssfRow.getCell(0);
HSSFCell name = hssfRow.getCell(1);
sb.append(no + ":" + name);
sb.append(";");
}
}
}
return sb.toString().substring(0, sb.toString().length() - 1);
}
示例2: test
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
@Test
public void test() throws IOException {
HSSFWorkbook workbook = createWorkbook();
byte[] buffer;
try (ByteArrayOutputStream stream = new ByteArrayOutputStream(1024)) {
workbook.write(stream);
stream.flush();
buffer = stream.toByteArray();
}
Map<String, BoundaryPoint> boundaryPoints;
try (InputStream stream = new ByteArrayInputStream(buffer)) {
BoundaryPointXlsParser parser = new BoundaryPointXlsParser();
boundaryPoints = parser.parse(stream);
}
assertEquals(1, boundaryPoints.size());
BoundaryPoint point = boundaryPoints.get("BoundaryPoint FR-BE");
assertNotNull(point);
assertEquals("BoundaryPoint FR-BE", point.getName());
assertEquals(Country.FR, point.getBorderFrom());
assertEquals(Country.BE, point.getBorderTo());
}
示例3: getCellStyle
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
/**
* 获取单元格式样式
*
* @param wb
* @param color
* @return
*/
private static HSSFCellStyle getCellStyle(HSSFWorkbook wb, int color) {
if (STYLE_MAP.get(color) != null) {
return STYLE_MAP.get(color);
}
HSSFCellStyle style = wb.createCellStyle();
if (color != -1) {
style.setFillForegroundColor(Short.valueOf(color + ""));
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setAlignment(CellStyle.ALIGN_CENTER);
}
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setTopBorderColor(HSSFColor.BLACK.index);
STYLE_MAP.put(color, style);
return style;
}
示例4: createColor
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
/**
* 创建颜色。
*
* 写本方法的原因是:从2003版本的模板中复制单元格颜色时,会出现颜色失真的问题。
*
* 但最终也没有解决。因为:当单元格的颜色设置为非标准颜色时,就会失真,但设置为标准颜色时,是正常的。
*
* 也因为此,本方法与 i_ToCellStyle.setFillBackgroundColor(i_FromCellStyle.getFillBackgroundColor()); 的效果是相同的。
*
* 本方法作为研究使用而保留下来,但不没有使用价值。
*
* @author ZhengWei(HY)
* @createDate 2017-03-21
* @version v1.0
*
* @param i_FromColor
* @param i_DataWorkbook
* @return
*/
@Deprecated
public final static HSSFColor createColor(HSSFColor i_FromColor ,HSSFWorkbook i_DataWorkbook)
{
short [] v_RGBHex = i_FromColor.getTriplet();
byte v_ByteRed = (byte)v_RGBHex[0];
byte v_ByteGreen = (byte)v_RGBHex[1];
byte v_ByteBlue = (byte)v_RGBHex[2];
HSSFPalette v_Palette = i_DataWorkbook.getCustomPalette();
HSSFColor v_DataColor = v_Palette.findColor(v_ByteRed ,v_ByteGreen ,v_ByteBlue);
if ( v_DataColor == null )
{
v_Palette.setColorAtIndex(i_FromColor.getIndex() ,v_ByteRed ,v_ByteGreen ,v_ByteBlue);
return v_Palette.getColor(i_FromColor.getIndex());
}
return v_DataColor;
}
示例5: insertItemToSheet
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
private void insertItemToSheet(String table, HSSFSheet sheet, ArrayList<String> columns) {
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
Cursor cursor = database.rawQuery("select * from " + table, null);
cursor.moveToFirst();
int n = 1;
while (!cursor.isAfterLast()) {
HSSFRow rowA = sheet.createRow(n);
for (int j = 0; j < columns.size(); j++) {
HSSFCell cellA = rowA.createCell(j);
if (cursor.getType(j) == Cursor.FIELD_TYPE_BLOB) {
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) j, n, (short) (j + 1), n + 1);
anchor.setAnchorType(3);
patriarch.createPicture(anchor, workbook.addPicture(cursor.getBlob(j), HSSFWorkbook.PICTURE_TYPE_JPEG));
} else {
cellA.setCellValue(new HSSFRichTextString(cursor.getString(j)));
}
}
n++;
cursor.moveToNext();
}
cursor.close();
}
示例6: createHSSFCellStyle
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
private HSSFCellStyle createHSSFCellStyle(Workbook wb, int[] bgColor, int[] fontColor, int fontSize) {
HSSFWorkbook workbook = (HSSFWorkbook) wb;
HSSFPalette palette = workbook.getCustomPalette();
palette.setColorAtIndex((short) 9, (byte) fontColor[0], (byte) fontColor[1], (byte) fontColor[2]);
palette.setColorAtIndex((short) 10, (byte) bgColor[0], (byte) bgColor[1], (byte) bgColor[2]);
HSSFFont titleFont = workbook.createFont();
titleFont.setCharSet(HSSFFont.DEFAULT_CHARSET);
titleFont.setFontName("宋体");
titleFont.setColor((short) 9);
titleFont.setBold(true);
titleFont.setFontHeightInPoints((short) fontSize);
HSSFCellStyle titleStyle = (HSSFCellStyle) createBorderCellStyle(workbook, true);
titleStyle.setFont(titleFont);
titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
titleStyle.setFillForegroundColor((short) 10);
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return titleStyle;
}
示例7: ImportExcel
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
/**
* 构造函数
*
* @param fileName 导入文件对象
* @param headerNum 标题行号,数据行号=标题行号+1
* @param sheetIndex 工作表编号
* @throws InvalidFormatException
* @throws IOException
*/
public ImportExcel(String fileName, InputStream is, int headerNum, int sheetIndex)
throws InvalidFormatException, IOException {
if (StringUtils.isBlank(fileName)) {
throw new RuntimeException("Import file is empty!");
} else if (fileName.toLowerCase().endsWith("xls")) {
this.wb = new HSSFWorkbook(is);
} else if (fileName.toLowerCase().endsWith("xlsx")) {
this.wb = new XSSFWorkbook(is);
} else {
throw new RuntimeException("Invalid import file type!");
}
if (this.wb.getNumberOfSheets() < sheetIndex) {
throw new RuntimeException("No sheet in Import file!");
}
this.sheet = this.wb.getSheetAt(sheetIndex);
this.headerNum = headerNum;
log.debug("Initialize success.");
}
示例8: setRegionBorder
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
/**
* 获取合并单元格式
*
* @param sheet
* @param row
* @param columnFrom
* @param columnTo
* @return
*/
private static void setRegionBorder(HSSFSheet sheet, int row, int columnFrom, int columnTo) {
CellRangeAddress region = new CellRangeAddress(row, row, columnFrom, columnTo);
sheet.addMergedRegion(region);
final short border = CellStyle.BORDER_THIN;
HSSFWorkbook wb = sheet.getWorkbook();
RegionUtil.setBorderBottom(border, region, sheet, wb);
RegionUtil.setBorderTop(border, region, sheet, wb);
RegionUtil.setBorderLeft(border, region, sheet, wb);
RegionUtil.setBorderRight(border, region, sheet, wb);
RegionUtil.setBottomBorderColor(HSSFColor.BLACK.index, region, sheet, wb);
RegionUtil.setTopBorderColor(HSSFColor.BLACK.index, region, sheet, wb);
RegionUtil.setLeftBorderColor(HSSFColor.BLACK.index, region, sheet, wb);
RegionUtil.setRightBorderColor(HSSFColor.BLACK.index, region, sheet, wb);
}
示例9: writeXLSFile
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
public static void writeXLSFile() throws IOException {
HSSFWorkbook wbObj = new HSSFWorkbook();
HSSFSheet sheet = wbObj.createSheet(sheetName);
for (int row = 0; row < tableData.size(); row++) {
HSSFRow rowObj = sheet.createRow(row);
rowData = tableData.get(row);
for (int col = 0; col < rowData.size(); col++) {
HSSFCell cellObj = rowObj.createCell(col);
cellObj.setCellValue(rowData.get(col));
}
}
FileOutputStream fileOut = new FileOutputStream(excelFileName);
wbObj.write(fileOut);
wbObj.close();
fileOut.flush();
fileOut.close();
}
示例10: parse
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
public Map<String, BoundaryPoint> parse(InputStream is) throws IOException {
Map<String, BoundaryPoint> boundaryPoints = new HashMap<>();
HSSFWorkbook workbook = new HSSFWorkbook(is);
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
rowIterator.next();
rowIterator.next();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Cell boundaryPointNameCell = row.getCell(13);
Cell borderFromCell = row.getCell(14);
Cell borderToCell = row.getCell(15);
String boundaryPointName = boundaryPointNameCell.getStringCellValue();
if (boundaryPointName.equals("-")) {
continue;
}
Country borderFrom = toCountry(borderFromCell.getStringCellValue());
Country borderTo = toCountry(borderToCell.getStringCellValue());
boundaryPoints.put(boundaryPointName, new BoundaryPoint(boundaryPointName, borderFrom, borderTo));
}
return boundaryPoints;
}
示例11: createWorkbook
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
private HSSFWorkbook createWorkbook() {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
// Add dummy lines
sheet.createRow(0).createCell(0).setCellValue("First dummy row");
sheet.createRow(1).createCell(0).setCellValue("Second dummy row");
HSSFRow row = sheet.createRow(2);
row.createCell(13).setCellValue("BoundaryPoint FR-BE");
row.createCell(14).setCellValue("France");
row.createCell(15).setCellValue("Belgium");
return workbook;
}
示例12: exportWorkbook
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
/**
* 导出Excel对象
*
* @param sheetDataListArr Excel数据
* @return
*/
public static Workbook exportWorkbook(List<?>... sheetDataListArr){
// data array valid
if (sheetDataListArr==null || sheetDataListArr.length==0) {
throw new RuntimeException(">>>>>>>>>>> xxl-excel error, data array can not be empty.");
}
// book (HSSFWorkbook=2003/xls、XSSFWorkbook=2007/xlsx)
Workbook workbook = new HSSFWorkbook();
// sheet
for (List<?> dataList: sheetDataListArr) {
makeSheet(workbook, dataList);
}
return workbook;
}
示例13: createExcel
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
public byte[] createExcel() throws IOException, ObjectNotFoundException {
String selectedLanguage = model.getSelectedLanguageCode();
Map<String, Properties> localizedProperties = new HashMap<String, Properties>();
List<POLocalizedData> poLocalizedDatas = getLocalizedDataService()
.exportProperties(selectedLanguage);
Workbook wb = new HSSFWorkbook();
List<Locale> locales = generateLocaleList();
for (POLocalizedData data : poLocalizedDatas) {
localizedProperties = data.getPropertiesMap();
if (data.getType().equals(LocalizedDataType.MessageProperties)) {
createSheet(localizedProperties, wb,
BaseBean.LABEL_USERINTERFACE_TRANSLARIONS, locales);
}
if (data.getType().equals(LocalizedDataType.MailProperties)) {
createSheet(localizedProperties, wb,
BaseBean.LABEL_MAIL_TRANSLARIONS, locales);
}
if (data.getType().equals(LocalizedDataType.PlatformObjects)) {
createSheet(localizedProperties, wb,
BaseBean.LABEL_PLATFORM_TRANSLARIONS, locales);
}
}
return createByteArrayForWorkbook(wb);
}
示例14: objectFrom
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
private Object objectFrom(final HSSFWorkbook workbook, final Cell cell) {
Object cellValue = null;
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
cellValue = cell.getRichStringCellValue().getString();
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
cellValue = getNumericCellValue(cell);
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
cellValue = cell.getBooleanCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
cellValue = evaluateCellFormula(workbook, cell);
}
return cellValue;
}
示例15: doGet
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
List<PollOption> results = null;
try {
Long pollId = (Long) req.getSession().getAttribute("pollID");
results = DAOProvider.getDao().getPollOptions(pollId);
} catch (Exception ex) {
results = new ArrayList<>();
}
HSSFWorkbook workbook = DBUtility.getXLS(results);
resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-Disposition",
"attachment; filename=votingResults.xls");
workbook.write(resp.getOutputStream());
workbook.close();
}