本文整理汇总了Java中org.apache.poi.hwpf.usermodel.Range类的典型用法代码示例。如果您正苦于以下问题:Java Range类的具体用法?Java Range怎么用?Java Range使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Range类属于org.apache.poi.hwpf.usermodel包,在下文中一共展示了Range类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: replaceWordDoc
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
public static void replaceWordDoc(String inPath, String outPath, Map<String, String> context) {
Validate.notBlank(inPath);
Validate.notBlank(outPath);
Validate.notNull(context);
try (FileInputStream in = new FileInputStream(new File(inPath));
FileOutputStream out = new FileOutputStream(outPath, false)) {
HWPFDocument hdt = new HWPFDocument(in);
Range range = hdt.getRange();
for (Map.Entry<String, String> entry : context.entrySet()) {
range.replaceText(entry.getKey(), entry.getValue());
}
hdt.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}
示例2: saveDoc
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
/**
* Method to save the file by parameters in doc format
*
* @param toSave The file where the information will be saved
*/
private void saveDoc(File toSave) {
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("down/empty.doc"));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
Paragraph parContainer = range.insertAfter(new ParagraphProperties(), 0);
for (String para : paragraphs) {
parContainer.setSpacingAfter(200);
parContainer.insertAfter(para);
parContainer = range.insertAfter(new ParagraphProperties(), 0);
}
FileOutputStream fos = new FileOutputStream(toSave);
doc.write(fos);
fos.close();
} catch (Exception e) {
Logger.getGlobal().log(Level.SEVERE, e.getMessage() + "\n" + Arrays.toString(e.getStackTrace()));
}
}
示例3: CreateDocFromTemplate
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
/**
* 创建Doc并保存
*
* @param templatePath 模板doc路径
* @param parameters 参数和值
* //* @param imageParameters 书签和图片
* @param savePath 保存doc的路径
* @return
*/
public static void CreateDocFromTemplate(String templatePath,
HashMap<String, String> parameters,
//HashMap<String, String> imageParameters,
String savePath)
throws Exception {
@Cleanup InputStream is = DocProducer.class.getResourceAsStream(templatePath);
HWPFDocument doc = new HWPFDocument(is);
Range range = doc.getRange();
//把range范围内的${}替换
for (Map.Entry<String, String> next : parameters.entrySet()) {
range.replaceText("${" + next.getKey() + "}",
next.getValue()
);
}
@Cleanup OutputStream os = new FileOutputStream(savePath);
//把doc输出到输出流中
doc.write(os);
}
示例4: testReadByDoc
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
@Test
public void testReadByDoc() throws Exception {
InputStream is = new FileInputStream("E://video/doc/xiuParty/90Xiu-NEW/oss/90秀--oss与服务器端接口文档.doc");
POIFSFileSystem pfilesystem = new POIFSFileSystem(is);
HWPFDocument doc = new HWPFDocument(pfilesystem);
// // 输出书签信息
// this.printInfo(doc.getBookmarks());
// // 输出文本
// System.out.println(doc.getDocumentText());
Range range = doc.getRange();
// this.insertInfo(range);
this.printInfo(range);
// 读表格
this.readTable(range);
// 读列表
this.readList(range);
// 删除range
Range r = new Range(2, 5, doc);
r.delete();// 在内存中进行删除,如果需要保存到文件中需要再把它写回文件
// 把当前HWPFDocument写到输出流中
doc.write(new FileOutputStream("D:\\test.doc"));
IOUtils.closeQuietly(is);
}
示例5: readTable
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
/**
* 读表格 每一个回车符代表一个段落,所以对于表格而言,每一个单元格至少包含一个段落,每行结束都是一个段落。
*
* @param range
*/
private void readTable(Range range) {
// 遍历range范围内的table。
TableIterator tableIter = new TableIterator(range);
Table table;
TableRow row;
TableCell cell;
while (tableIter.hasNext()) {
table = tableIter.next();
int rowNum = table.numRows();
for (int j = 0; j < rowNum; j++) {
row = table.getRow(j);
int cellNum = row.numCells();
for (int k = 0; k < cellNum; k++) {
cell = row.getCell(k);
// 输出单元格的文本
System.out.println(cell.text().trim());
}
}
}
}
示例6: printInfo
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
/**
* 输出Range
*
* @param range
*/
private void printInfo(Range range) {
// 获取段落数
int paraNum = range.numParagraphs();
System.out.println(paraNum);
for (int i = 0; i < paraNum; i++) {
// this.insertInfo(range.getParagraph(i));
System.out.println("段落" + (i + 1) + ":" + range.getParagraph(i).text());
if (i == (paraNum - 1)) {
this.insertInfo(range.getParagraph(i));
}
}
int secNum = range.numSections();
System.out.println(secNum);
Section section;
for (int i = 0; i < secNum; i++) {
section = range.getSection(i);
System.out.println(section.getMarginLeft());
System.out.println(section.getMarginRight());
System.out.println(section.getMarginTop());
System.out.println(section.getMarginBottom());
System.out.println(section.getPageHeight());
System.out.println(section.text());
}
}
示例7: handleHeaderFooter
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
private void handleHeaderFooter(Range[] ranges, String type,
HWPFDocument document, PicturesSource pictures,
PicturesTable pictureTable, XHTMLContentHandler xhtml)
throws SAXException, IOException, TikaException {
if (countParagraphs(ranges) > 0) {
xhtml.startElement("div", "class", type);
for (Range r : ranges) {
if (r != null) {
for (int i = 0; i < r.numParagraphs(); i++) {
Paragraph p = r.getParagraph(i);
String text = p.text();
if (text.replaceAll("[\\r\\n\\s]+", "").isEmpty()) {
// Skip empty header or footer paragraphs
} else {
i += handleParagraph(p, 0, r, document,
FieldsDocumentPart.HEADER, pictures, pictureTable, xhtml);
}
}
}
}
xhtml.endElement("div");
}
}
示例8: processBookmarks
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
@Override
protected void processBookmarks( HWPFDocumentCore wordDocument,
Element currentBlock, Range range, int currentTableLevel,
List<Bookmark> rangeBookmarks )
{
Element parent = currentBlock;
for ( Bookmark bookmark : rangeBookmarks )
{
Element bookmarkElement = foDocumentFacade.createInline();
final String idName = "bookmark_" + bookmark.getName();
// make sure ID used once
if ( setId( bookmarkElement, idName ) )
{
/*
* if it just empty fo:inline without "id" attribute doesn't
* making sense to add it to DOM
*/
parent.appendChild( bookmarkElement );
parent = bookmarkElement;
}
}
if ( range != null )
processCharacters( wordDocument, currentTableLevel, range, parent );
}
示例9: processBookmarks
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
@Override
protected void processBookmarks( HWPFDocumentCore wordDocument,
Element currentBlock, Range range, int currentTableLevel,
List<Bookmark> rangeBookmarks )
{
Element parent = currentBlock;
for ( Bookmark bookmark : rangeBookmarks )
{
Element bookmarkElement = htmlDocumentFacade
.createBookmark( bookmark.getName() );
parent.appendChild( bookmarkElement );
parent = bookmarkElement;
}
if ( range != null )
processCharacters( wordDocument, currentTableLevel, range, parent );
}
示例10: processNote
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
protected void processNote( HWPFDocument wordDocument, Element block,
Range noteTextRange )
{
final int noteIndex = noteCounters.getAndIncrement();
block.appendChild( textDocumentFacade
.createText( UNICODECHAR_ZERO_WIDTH_SPACE + "[" + noteIndex
+ "]" + UNICODECHAR_ZERO_WIDTH_SPACE ) );
if ( notes == null )
notes = textDocumentFacade.createBlock();
Element note = textDocumentFacade.createBlock();
notes.appendChild( note );
note.appendChild( textDocumentFacade.createText( "^" + noteIndex
+ "\t " ) );
processCharacters( wordDocument, Integer.MIN_VALUE, noteTextRange, note );
note.appendChild( textDocumentFacade.createText( "\n" ) );
}
示例11: getParagraphText
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
/**
* Get the text from the word file, as an array with one String
* per paragraph
*/
@Deprecated
public String[] getParagraphText() {
String[] ret;
// Extract using the model code
try {
Range r = doc.getRange();
ret = WordExtractor.getParagraphText(r);
} catch (Exception e) {
// Something's up with turning the text pieces into paragraphs
// Fall back to ripping out the text pieces
ret = new String[doc.getTextTable().getTextPieces().size()];
for(int i=0; i<ret.length; i++) {
ret[i] = doc.getTextTable().getTextPieces().get(i).getStringBuilder().toString();
// Fix the line endings
ret[i].replaceAll("\r", "\ufffe");
ret[i].replaceAll("\ufffe","\r\n");
}
}
return ret;
}
示例12: getParagraphText
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
/**
* Get the text from the word file, as an array with one String per
* paragraph
*/
public String[] getParagraphText()
{
String[] ret;
// Extract using the model code
try
{
Range r = doc.getRange();
ret = getParagraphText( r );
}
catch ( Exception e )
{
// Something's up with turning the text pieces into paragraphs
// Fall back to ripping out the text pieces
ret = new String[1];
ret[0] = getTextFromPieces();
}
return ret;
}
示例13: getAllPictures
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
/**
* Not all documents have all the images concatenated in the data stream
* although MS claims so. The best approach is to scan all character runs.
*
* @return a list of Picture objects found in current document
*/
public List<Picture> getAllPictures() {
ArrayList<Picture> pictures = new ArrayList<Picture>();
Range range = _document.getOverallRange();
for (int i = 0; i < range.numCharacterRuns(); i++) {
CharacterRun run = range.getCharacterRun(i);
if (run==null) {
continue;
}
Picture picture = extractPicture(run, false);
if (picture != null) {
pictures.add(picture);
}
}
searchForPictures(_dgg.getEscherRecords(), pictures);
return pictures;
}
示例14: processParagraphRequest
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
/**
* Processing of text requests.
*
* @param doc word document
* @param paragraphRef reference to paragraphs
* @return list of text values as {@code LinkedList<Value>}
*/
private LinkedList<Value> processParagraphRequest(HWPFDocument doc, String paraRef) {
LinkedList<Value> valList = new LinkedList<Value>();
Range docRange = doc.getRange();
setStartEndParagraph(paraRef, "*");
if (this.startPara == -1 || this.endPara == -1) {
this.startPara = 0;
this.endPara = docRange.numParagraphs() - 1;
}
// -1 => all paragraphs requested
if (this.startPara < docRange.numParagraphs()) {
if (!(this.endPara < docRange.numParagraphs())) {
this.endPara = docRange.numParagraphs() - 1;
}
for (Integer pPos = this.startPara; pPos <= this.endPara; pPos++) {
Paragraph p = docRange.getParagraph(pPos);
Integer subURI = pPos + 1;
valList.add(getParagraph(p, subURI.toString()));
}
}
return valList;
}
示例15: read
import org.apache.poi.hwpf.usermodel.Range; //导入依赖的package包/类
public ArrayList<TableData> read(String path) throws IOException {
FileInputStream in = new FileInputStream(new File(path));
HWPFDocument hwpf = new HWPFDocument(in);
Range range = hwpf.getRange();// 得到文档的读取范围
TableIterator it = new TableIterator(range);
ArrayList<TableData> list=new ArrayList<TableData>();
int count=0;
// 迭代文档中的表格
while (it.hasNext()) {
Table tb = (Table) it.next();
TableData data=new TableData();
// 迭代行,默认从0开始
for (int i = 0; i < tb.numRows(); i++) {
TableRow tr = tb.getRow(i);
// 迭代列,默认从0开始
for (int j = 0; j < tr.numCells(); j++) {
TableCell td = tr.getCell(j);// 取得单元格
// 取得单元格的内容
StringBuffer sb=new StringBuffer();
for (int k = 0; k < td.numParagraphs(); k++) {
Paragraph para = td.getParagraph(k);
sb.append(para.text());
}
String string=sb.toString().trim();
if(i==0&&j==1)
{
data.setClazz(string);
}
else if(i==1&&j==1)
{
data.setName(string);
}
else if(i==2&&j==1)
{
data.setLifeCircle(string);
}
else if(i==2&&j==3)
{
data.setTheme(string);
}
else if(i==3&&j==3)
{
data.setForm(string);
}
else if(i==4&&j==1)
{
data.setKeywords(string);
}
else if(i==9&&j==1)
{
data.setContent(string);
}
}
}
// show("第"+count+"条:"+data.toString());
count+=1;
list.add(data);
}
return list;
}