|
POIµÄ¼ò½é
Apache POIÊÇApacheÈí¼þ»ù½ð»áµÄ¿ª·ÅÔ´Â뺯ʽ¿â£¬POIÌṩAPI¸øJava³ÌÐò¶ÔMicrosoft Office¸ñʽµµ°¸¶ÁºÍдµÄ¹¦ÄÜ¡£Poor Obfuscation Implementation¼´ ¼ò½à°æµÄÄ£ºýʵÏÖ¡£
1¡¢»ù±¾¹¦ÄÜ POIµÄ°²×°
ÏÂÔصØÖ·£ºhttps://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-4.0.0-20180907.tar.gz
% gpg --import KEYS
% gpg --verify downloaded_file.asc downloaded_file
or
% pgpk -a KEYS
% pgpv downloaded_file.asc
or
% pgp -ka KEYS
% pgp downloaded_file.asc |
POIµÄʹÓ÷½·¨(»ùÓÚPOIµÄת»»¡ªWord¡¢Excel¡¢PptµÈתhtml) 1¡¢»ù´¡°¸Àý 1.1¡¢ ´´½¨Excel Îĵµ¡ªÀûÓÃJakarta POI API ´´½¨Excel Îĵµ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream;
public class CreateXL {
/** Excel ÎļþÒª´æ·ÅµÄλÖ㬼ٶ¨ÔÚDÅÌÏÂ*/
public static String outputFile = "D:\\test.xls";
public static void main(String argv[]) {
try {
// ´´½¨ÐµÄExcel ¹¤×÷²¾
HSSFWorkbook workbook = new HSSFWorkbook();
// ÔÚExcel¹¤×÷²¾Öн¨Ò»¹¤×÷±í£¬ÆäÃûΪȱʡֵ
// ÈçҪн¨Ò»ÃûΪ"ЧÒæÖ¸±ê"µÄ¹¤×÷±í£¬ÆäÓï¾äΪ£º
// HSSFSheet sheet = workbook.createSheet("ЧÒæÖ¸±ê");
HSSFSheet sheet = workbook.createSheet();
// ÔÚË÷Òý0µÄλÖô´½¨ÐУ¨×¶ËµÄÐУ©
HSSFRow row = sheet.createRow((short)0);
//ÔÚË÷Òý0µÄλÖô´½¨µ¥Ôª¸ñ£¨×óÉ϶ˣ©
HSSFCell cell = row.createCell((short)0);
// ¶¨Òåµ¥Ôª¸ñΪ×Ö·û´®ÀàÐÍ
cell.setCellType(HSSFCell.CELL_TYPE_STRING);//Òѹýʱ
// ÔÚµ¥Ôª¸ñÖÐÊäÈëһЩÄÚÈÝ
cell.setCellValue("Ôö¼ÓÖµ");
// н¨Ò»Êä³öÎļþÁ÷
FileOutputStream fOut = new FileOutputStream(outputFile);
// °ÑÏàÓ¦µÄExcel ¹¤×÷²¾´æÅÌ
workbook.write(fOut);
fOut.flush();
// ²Ù×÷½áÊø£¬¹Ø±ÕÎļþ
fOut.close();
System.out.println("ÎļþÉú³É...");
} catch (Exception e) {
System.out.println("ÒÑÔËÐÐ xlCreate() : " + e);
}
}
} |
1.2¡¢¶ÁÈ¡ExcelÎĵµÖеÄÊý¾Ý ¶ÁÈ¡ExcelÎĵµÖеÄÊý¾Ý¡£¼Ù¶¨ÔÚDÅÌJTestĿ¼ÏÂÓÐÒ»¸öÎļþÃûΪtest1.xlsµÄExcelÎļþ¡£
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
public class ReadXL {
/** ExcelÎļþµÄ´æ·ÅλÖá£×¢ÒâÊÇ·´Ð±Ïß*/
public static String fileToBeRead = "D:\\test1.xls";
public static void main(String argv[]) {
try {
// ´´½¨¶ÔExcel¹¤×÷²¾ÎļþµÄÒýÓÃ
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
// ´´½¨¶Ô¹¤×÷±íµÄÒýÓá£
// ±¾ÀýÊÇ°´ÃûÒýÓã¨ÈÃÎÒÃǼٶ¨ÄÇÕűíÓÐ×ÅȱʡÃû"Sheet1"£©
HSSFSheet sheet = workbook.getSheet("Sheet1");
// Ò²¿ÉÓÃgetSheetAt(int index)°´Ë÷ÒýÒýÓã¬
// ÔÚExcelÎĵµÖУ¬µÚÒ»ÕŹ¤×÷±íµÄȱʡË÷ÒýÊÇ0£¬
// ÆäÓï¾äΪ£ºHSSFSheet sheet = workbook.getSheetAt(0);
// ¶ÁÈ¡×óÉ϶˵¥Ôª
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell((short)0);
// Êä³öµ¥ÔªÄÚÈÝ£¬cell.getStringCellValue()¾ÍÊÇÈ¡ËùÔÚµ¥ÔªµÄÖµ
System.out.println("×óÉ϶˵¥ÔªÊÇ£º " + cell.getStringCellValue());
} catch (Exception e) {
System.out.println("ÒÑÔËÐÐxlRead() : " + e);
}
}
} |
1.3¡¢´¦ÀíWORDÎĵµ import java.io. * ;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class TestPoi {
public TestPoi() {}
public static void main(String args[])throws Exception{
FileInputStream in = new FileInputStream("D:\\a.doc");
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText(in);
//System.out.println("the result length is"+str.length());
System.out.println(str);
}
} |
2¡¢½ø½×°¸ÀýÓ¦Óà ÎÄÕ²ο¼£º
https://blog.csdn.net/yjclsx/article/details/51441632
http://blog.csdn.net/z69183787/article/details/17468039
ÔÚÏÖÔÚÕâ¸ö»¥ÁªÍøÔÚÏßʱ´ú£¬javaʵÏÖÔÚÏßÔ¤ÀÀµÄÕâ¸öÐèÇ󣬻òÐíÊÇ´ó¼Ò¶¼ÏëÒªµÄ¡£µ±È»£¬Èç¹û¹«Ë¾ÓÐÇ®£¬Ö±½ÓʹÓø¶·ÑµÄµÚÈý·½Èí¼þ»òÕßÔÆÔÚÏßÔ¤ÀÀ·þÎñ¾Í¿ÉÒÔÁË£¬±ÈÈçÓÀÖÐoffice¡¢office web 365(http://www.officeweb365.com/)µÈ¹«Ë¾£¬¶¼ÓÐÔƶËÔÚÏßÔ¤ÀÀ·þÎñ£¬¿ÉÏë¶øÖª£¬ÕâЩÀ©Õ¹¹¦ÄÜ£¬¿Ï¶¨ÊÇÐèҪǮÁË¡£
µ«ÊÇ£¬¶ÔÓÚÎÒÃÇѧÉúµ³£¬Èç¹ûÏëÒªÃâ·Ñ»òÕßDIY£¬Ôò¿ÉÒÔÓÃopenoffice£¬»¹ÐèÒª½èÖúÆäËûµÄ¹¤¾ß(ÀýÈçswfTools¡¢FlexPaperµÈ)²ÅÐС£ÊµÏÖÔÀí¾ÍÊÇ£º
- 1.ͨ¹ýµÚÈý·½¹¤¾ßopenoffice£¬½«word¡¢excel¡¢ppt¡¢txtµÈÎļþת»»ÎªpdfÎļþ£»
- 2.ͨ¹ýswfTools½«pdfÎļþת»»³Éswf¸ñʽµÄÎļþ£»
- 3.ͨ¹ýFlexPaperÎĵµ×é¼þÔÚÒ³ÃæÉϽøÐÐչʾ¡£
µ±È»Èç¹û×°ÁËAdobe Reader XI£¬ÄÇ°ÑpdfÖ±½ÓÍϵ½ä¯ÀÀÆ÷Ò³Ãæ¾Í¿ÉÒÔÖ±½Ó´ò¿ªÔ¤ÀÀ£¬ÕâÑù¾Í²»ÐèÒª²½Öè2¡¢3ÁË£¬Ç°Ìá¾ÍÊÇ¿Í»§×°ÁËAdobe Reader XIÕâ¸öpdfÔĶÁÆ÷¡£
µ±È»£¬±¾ÆªÎÄÕÂÖ÷Òª½éÉܵÄPOI£¬ËùÒÔ£¬¿ÉÒÔͨ¹ýPOIʵÏÖword¡¢excel¡¢pptתhtml£¬ÕâÑù¾Í¿ÉÒÔ·ÅÔÚÒ³ÃæÉÏÁË¡£
2.1¡¢wordתhtml package wordToHtml;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.w3c.dom.Document;
public class PoiWordToHtml {
public static void main(String[] args) throws Throwable {
final String path = "D:\\poi-test\\wordToHtml\\";
final String file = "ÈËԱѡÔñϵ·Ö.doc";
InputStream input = new FileInputStream(path + file);
HWPFDocument wordDocument = new HWPFDocument(input);
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
wordToHtmlConverter.setPicturesManager(new PicturesManager() {
public String savePicture(byte[] content, PictureType pictureType,
String suggestedName, float widthInches, float heightInches) {
return suggestedName;
}
});
wordToHtmlConverter.processDocument(wordDocument);
List pics = wordDocument.getPicturesTable().getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get(i);
try {
pic.writeImageContent(new FileOutputStream(path
+ pic.suggestFullFileName()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
outStream.close();
String content = new String(outStream.toByteArray());
FileUtils.writeStringToFile(new File(path, "ÈËԱѡÔñϵ·Ö.html"), content, "utf-8");
}
} |
2.2¡¢excelתhtml package excelToHtml;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.converter.ExcelToHtmlConverter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.w3c.dom.Document;
public class PoiExcelToHtml {
final static String path = "D:\\poi-test\\excelToHtml\\";
final static String file = "exportExcel.xls";
public static void main(String args[]) throws Exception {
InputStream input=new FileInputStream(path+file);
HSSFWorkbook excelBook=new HSSFWorkbook(input);
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter (
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument() );
excelToHtmlConverter.processWorkbook(excelBook);
List pics = excelBook.getAllPictures();
if (pics != null) {
for (int i = 0; i < pics.size(); i++) {
Picture pic = (Picture) pics.get (i);
try {
pic.writeImageContent (new FileOutputStream (path + pic.suggestFullFileName() ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Document htmlDocument =excelToHtmlConverter.getDocument();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource (htmlDocument);
StreamResult streamResult = new StreamResult (outStream);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty (OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty (OutputKeys.INDENT, "yes");
serializer.setOutputProperty (OutputKeys.METHOD, "html");
serializer.transform (domSource, streamResult);
outStream.close();
String content = new String (outStream.toByteArray() );
FileUtils.writeStringToFile(new File (path, "exportExcel.html"), content, "utf-8");
}
} |
2.3¡¢pptתhtml ÆäʵֻÊÇpptתͼƬ£¬ÓÐÁËͼƬºó·Åµ½Ò³ÃæÉÏÈ¥£¬µã»÷ÏÂÒ»Ò³¾ÍÒ»ÕÅÕÅÏÔʾ¾Í¿ÉÒÔÁË¡£ÕâÀïÖ»½éÉÜpptתͼƬµÄ¹ý³Ì¡£pptתͼƬÓиöȱÏÝ£¬¾ÍÊÇpptÀï²»ÊÇËÎÌåµÄ×ÖÓÐЩ¿ÉÄÜ»á±ä³É¿ò¿ò¡£ÒÔÉ϶¼ÐèÒªÒýÈëpoiµÄjar°ü¡£ÒªÊµÏÖÔÚÏßÔ¤ÀÀ£¬Ö»Ðè°Ñת»»µÃµ½µÄhtmlÔÚбêÇ©Ò³´ò¿ª»òÕßÏâǶµ½Ä³¿éÇøÓò¾Í¿ÉÒÔÕ¹ÏÖÁË¡£
package pptToImg;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.record.Slide;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
public class PPTtoImage {
public static void main(String[] args) {
// ¶ÁÈëPPTÎļþ
File file = new File("D:/poi-test/pptToImg/test.ppt");
doPPTtoImage(file);
}
public static boolean doPPTtoImage(File file) {
boolean isppt = checkFile(file);
if (!isppt) {
System.out.println("The image you specify don't exit!");
return false;
}
try {
FileInputStream is = new FileInputStream(file);
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
System.out.print("µÚ" + i + "Ò³¡£");
TextRun[] truns = slide[i].getTextRuns();
for ( int k=0;k<truns.length;k++){
RichTextRun[] rtruns = truns[k].getRichTextRuns();
for(int l=0;l<rtruns.length;l++){
int index = rtruns[l].getFontIndex();
String name = rtruns[l].getFontName();
rtruns[l].setFontIndex(1);
rtruns[l].setFontName("ËÎÌå");
// System.out.println(rtruns[l].getText());
}
}
BufferedImage img = new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setPaint(Color.BLUE);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
// ÕâÀïÉèÖÃͼƬµÄ´æ·Å·¾¶ºÍͼƬµÄ¸ñʽ(jpeg,png,bmpµÈµÈ),×¢ÒâÉú³ÉÎļþ·¾¶
FileOutputStream out = new FileOutputStream("D:/poi-test/pptToImg/pict_"+ (i + 1) + ".jpeg");
javax.imageio.ImageIO.write(img, "jpeg", out);
out.close();
}
System.out.println("success!!");
return true;
} catch (FileNotFoundException e) {
System.out.println(e);
// System.out.println("Can't find the image!");
} catch (IOException e) {
}
return false;
}
// function ¼ì²éÎļþÊÇ·ñΪPPT
public static boolean checkFile(File file) {
boolean isppt = false;
String filename = file.getName();
String suffixname = null;
if (filename != null && filename.indexOf(".") != -1) {
suffixname = filename.substring(filename.indexOf("."));
if (suffixname.equals(".ppt")) {
isppt = true;
}
return isppt;
} else {
return isppt;
}
}
} |
----------------------------
ÔÎÄÁ´½Ó:https://blog.51cto.com/u_14217737/2905375
³ÌÐòÔ³µÄ¼¼Êõ´ó¹ÛÔ°£ºwww.javathinker.net
[Õâ¸öÌù×Ó×îºóÓÉ flybird ÔÚ 2021-06-23 14:51:16 ÖØбà¼]
|
|