use of org.apache.poi.hssf.usermodel.HSSFWorkbook in project poi by apache.
the class DataExtraction method main.
public static void main(String[] args) throws Exception {
if (args.length == 0) {
usage();
return;
}
FileInputStream is = new FileInputStream(args[0]);
HSLFSlideShow ppt = new HSLFSlideShow(is);
is.close();
//extract all sound files embedded in this presentation
HSLFSoundData[] sound = ppt.getSoundData();
for (int i = 0; i < sound.length; i++) {
//*.wav
String type = sound[i].getSoundType();
//typically file name
String name = sound[i].getSoundName();
//raw bytes
byte[] data = sound[i].getData();
//save the sound on disk
FileOutputStream out = new FileOutputStream(name + type);
out.write(data);
out.close();
}
int oleIdx = -1, picIdx = -1;
for (HSLFSlide slide : ppt.getSlides()) {
//extract embedded OLE documents
for (HSLFShape shape : slide.getShapes()) {
if (shape instanceof OLEShape) {
oleIdx++;
OLEShape ole = (OLEShape) shape;
HSLFObjectData data = ole.getObjectData();
String name = ole.getInstanceName();
if ("Worksheet".equals(name)) {
//read xls
@SuppressWarnings({ "unused", "resource" }) HSSFWorkbook wb = new HSSFWorkbook(data.getData());
} else if ("Document".equals(name)) {
HWPFDocument doc = new HWPFDocument(data.getData());
//read the word document
Range r = doc.getRange();
for (int k = 0; k < r.numParagraphs(); k++) {
Paragraph p = r.getParagraph(k);
System.out.println(p.text());
}
//save on disk
FileOutputStream out = new FileOutputStream(name + "-(" + (oleIdx) + ").doc");
doc.write(out);
out.close();
doc.close();
} else {
FileOutputStream out = new FileOutputStream(ole.getProgID() + "-" + (oleIdx + 1) + ".dat");
InputStream dis = data.getData();
byte[] chunk = new byte[2048];
int count;
while ((count = dis.read(chunk)) >= 0) {
out.write(chunk, 0, count);
}
is.close();
out.close();
}
} else //Pictures
if (shape instanceof HSLFPictureShape) {
picIdx++;
HSLFPictureShape p = (HSLFPictureShape) shape;
HSLFPictureData data = p.getPictureData();
String ext = data.getType().extension;
FileOutputStream out = new FileOutputStream("pict-" + picIdx + ext);
out.write(data.getData());
out.close();
}
}
}
ppt.close();
}
use of org.apache.poi.hssf.usermodel.HSSFWorkbook in project poi by apache.
the class CalendarDemo method main.
public static void main(String[] args) throws Exception {
Calendar calendar = Calendar.getInstance();
boolean xlsx = true;
for (int i = 0; i < args.length; i++) {
if (args[i].charAt(0) == '-') {
xlsx = args[i].equals("-xlsx");
} else {
calendar.set(Calendar.YEAR, Integer.parseInt(args[i]));
}
}
int year = calendar.get(Calendar.YEAR);
Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook();
Map<String, CellStyle> styles = createStyles(wb);
for (int month = 0; month < 12; month++) {
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, 1);
//create a sheet for each month
Sheet sheet = wb.createSheet(months[month]);
//turn off gridlines
sheet.setDisplayGridlines(false);
sheet.setPrintGridlines(false);
sheet.setFitToPage(true);
sheet.setHorizontallyCenter(true);
PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true);
//the following three statements are required only for HSSF
sheet.setAutobreaks(true);
printSetup.setFitHeight((short) 1);
printSetup.setFitWidth((short) 1);
//the header row: centered text in 48pt font
Row headerRow = sheet.createRow(0);
headerRow.setHeightInPoints(80);
Cell titleCell = headerRow.createCell(0);
titleCell.setCellValue(months[month] + " " + year);
titleCell.setCellStyle(styles.get("title"));
sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$N$1"));
//header with month titles
Row monthRow = sheet.createRow(1);
for (int i = 0; i < days.length; i++) {
//set column widths, the width is measured in units of 1/256th of a character width
//the column is 5 characters wide
sheet.setColumnWidth(i * 2, 5 * 256);
//the column is 13 characters wide
sheet.setColumnWidth(i * 2 + 1, 13 * 256);
sheet.addMergedRegion(new CellRangeAddress(1, 1, i * 2, i * 2 + 1));
Cell monthCell = monthRow.createCell(i * 2);
monthCell.setCellValue(days[i]);
monthCell.setCellStyle(styles.get("month"));
}
int cnt = 1, day = 1;
int rownum = 2;
for (int j = 0; j < 6; j++) {
Row row = sheet.createRow(rownum++);
row.setHeightInPoints(100);
for (int i = 0; i < days.length; i++) {
Cell dayCell_1 = row.createCell(i * 2);
Cell dayCell_2 = row.createCell(i * 2 + 1);
int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
if (cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) {
dayCell_1.setCellValue(day);
calendar.set(Calendar.DAY_OF_MONTH, ++day);
if (i == 0 || i == days.length - 1) {
dayCell_1.setCellStyle(styles.get("weekend_left"));
dayCell_2.setCellStyle(styles.get("weekend_right"));
} else {
dayCell_1.setCellStyle(styles.get("workday_left"));
dayCell_2.setCellStyle(styles.get("workday_right"));
}
} else {
dayCell_1.setCellStyle(styles.get("grey_left"));
dayCell_2.setCellStyle(styles.get("grey_right"));
}
cnt++;
}
if (calendar.get(Calendar.MONTH) > month)
break;
}
}
// Write the output to a file
String file = "calendar.xls";
if (wb instanceof XSSFWorkbook)
file += "x";
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
wb.close();
}
use of org.apache.poi.hssf.usermodel.HSSFWorkbook in project poi by apache.
the class CellStyleDetails method main.
public static void main(String[] args) throws Exception {
if (args.length == 0) {
throw new IllegalArgumentException("Filename must be given");
}
Workbook wb = WorkbookFactory.create(new File(args[0]));
DataFormatter formatter = new DataFormatter();
for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
Sheet sheet = wb.getSheetAt(sn);
System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());
for (Row row : sheet) {
System.out.println(" Row " + row.getRowNum());
for (Cell cell : row) {
CellReference ref = new CellReference(cell);
System.out.print(" " + ref.formatAsString());
System.out.print(" (" + cell.getColumnIndex() + ") ");
CellStyle style = cell.getCellStyle();
System.out.print("Format=" + style.getDataFormatString() + " ");
System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");
Font font = wb.getFontAt(style.getFontIndex());
System.out.print("Font=" + font.getFontName() + " ");
System.out.print("FontColor=");
if (font instanceof HSSFFont) {
System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
}
if (font instanceof XSSFFont) {
System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
}
System.out.println();
System.out.println(" " + formatter.formatCellValue(cell));
}
}
System.out.println();
}
wb.close();
}
use of org.apache.poi.hssf.usermodel.HSSFWorkbook in project poi by apache.
the class AddDimensionedImage method main.
/**
* The main entry point to the program. It contains code that demonstrates
* one way to use the program.
*
* Note, the code is not restricted to use on new workbooks only. If an
* image is to be inserted into an existing workbook. just open that
* workbook, gat a reference to a sheet and pass that;
*
* AddDimensionedImage addImage = new AddDimensionedImage();
*
* File file = new File("....... Existing Workbook .......");
* FileInputStream fis = new FileInputStream(file);
* Workbook workbook = new HSSFWorkbook(fis);
* HSSFSheet sheet = workbook.getSheetAt(0);
* addImage.addImageToSheet("C3", sheet, "image.jpg", 30, 20,
* AddDimensionedImage.EXPAND.ROW);
*
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
String imageFile = null;
String outputFile = null;
FileOutputStream fos = null;
Workbook workbook = null;
Sheet sheet = null;
if (args.length < 2) {
System.err.println("Usage: AddDimensionedImage imageFile outputFile");
return;
}
// OR XSSFWorkbook
workbook = new HSSFWorkbook();
sheet = workbook.createSheet("Picture Test");
imageFile = args[0];
outputFile = args[1];
new AddDimensionedImage().addImageToSheet("B5", sheet, sheet.createDrawingPatriarch(), new File(imageFile).toURI().toURL(), 100, 40, AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
fos = new FileOutputStream(outputFile);
workbook.write(fos);
fos.close();
workbook.close();
}
use of org.apache.poi.hssf.usermodel.HSSFWorkbook in project poi by apache.
the class LoadEmbedded method loadEmbedded.
public static void loadEmbedded(XSSFWorkbook workbook) throws IOException, InvalidFormatException, OpenXML4JException, XmlException {
for (PackagePart pPart : workbook.getAllEmbedds()) {
String contentType = pPart.getContentType();
if (contentType.equals("application/vnd.ms-excel")) {
// Excel Workbook - either binary or OpenXML
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
embeddedWorkbook.close();
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
// Excel Workbook - OpenXML file format
XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
embeddedWorkbook.close();
} else if (contentType.equals("application/msword")) {
// Word Document - binary (OLE2CDF) file format
HWPFDocument document = new HWPFDocument(pPart.getInputStream());
document.close();
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
// Word Document - OpenXML file format
XWPFDocument document = new XWPFDocument(pPart.getInputStream());
document.close();
} else if (contentType.equals("application/vnd.ms-powerpoint")) {
// PowerPoint Document - binary file format
HSLFSlideShow slideShow = new HSLFSlideShow(pPart.getInputStream());
slideShow.close();
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
// PowerPoint Document - OpenXML file format
XMLSlideShow slideShow = new XMLSlideShow(pPart.getInputStream());
slideShow.close();
} else {
// Any other type of embedded object.
System.out.println("Unknown Embedded Document: " + contentType);
InputStream inputStream = pPart.getInputStream();
inputStream.close();
}
}
}
Aggregations