本文整理匯總了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();
}