Chinaunix
标题:
将html内容生成为word文档实现思路 2.................
[打印本页]
作者:
feiyang10086
时间:
2011-12-13 17:07
标题:
将html内容生成为word文档实现思路 2.................
将html内容生成为word文档实现思路2....................
return this.tables; this.tables = Dispatch.get(this.document, "Tables").toDispatch(); return this.tables; } /** * 获取当前文档中的所有表格数量 * * @return 表格数量 */ public int getTablesCount() { if (this.tables == null) this.getTables(); return Dispatch.get(tables, "Count").getInt(); } /** * 根据索引获得table对象 * * @param tableIndex * 索引 * @return table */ public Dispatch getTable(int tableIndex) { if (this.tables == null) this.getTables(); if (tableIndex >= 0) this.table = Dispatch.call(this.tables, "Item", new Variant(tableIndex)).toDispatch(); return this.table; } /** * 在指定的单元格里填写数据 * * @param tableIndex * 表格索引 * @param cellRowIdx * 行索引 * @param cellColIdx * 列索引 * @param txt * 文本 */ public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx, String txt) { getTable(tableIndex); getCell(cellRowIdx, cellColIdx); Dispatch.call(this.cell, "Select"); Dispatch.put(this.selection, "Text", txt); } /** * 在当前文档末尾拷贝来自另一个文档中的段落 * * @param anotherDocPath * 另一个文档的磁盘路径 * @param tableIndex * 被拷贝的段落在另一格文档中的序号(从1开始) */ public void copyParagraphFromAnotherDoc(String anotherDocPath, int paragraphIndex) { Dispatch wordContent = Dispatch.get(this.document, "Content").toDispatch(); // 取得当前文档的内容 Dispatch.call(wordContent, "InsertAfter", "$selection$");// 插入特殊符定位插入点 copyParagraphFromAnotherDoc(anotherDocPath, paragraphIndex, "$selection$"); } /** * 在当前文档指定的位置拷贝来自另一个文档中的段落 * * @param anotherDocPath * 另一个文档的磁盘路径 * @param tableIndex * 被拷贝的段落在另一格文档中的序号(从1开始) * @param pos * 当前文档指定的位置 */ public void copyParagraphFromAnotherDoc(String anotherDocPath, int paragraphIndex, String pos) { Dispatch doc2 = null; try { doc2 = Dispatch.call(documents, "Open", anotherDocPath).toDispatch(); Dispatch paragraphs = Dispatch.get(doc2, "Paragraphs").toDispatch(); Dispatch paragraph = Dispatch.call(paragraphs, "Item", new Variant(paragraphIndex)).toDispatch(); Dispatch range = Dispatch.get(paragraph, "Range").toDispatch(); Dispatch.call(range, "Copy"); if (this.find(pos)) { getRange(); Dispatch.call(this.range, "Paste"); } } catch (Exception e) { e.printStackTrace(); } finally { if (doc2 != null) { Dispatch.call(doc2, "Close", new Variant(true)); doc2 = null; } } } /** * 在当前文档指定的位置拷贝来自另一个文档中的表格 * * @param anotherDocPath * 另一个文档的磁盘路径 * @param tableIndex * 被拷贝的表格在另一格文档中的序号(从1开始) * @param pos * 当前文档指定的位置 */ public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex, String pos) { Dispatch doc2 = null; try { doc2 = Dispatch.call(documents, "Open", anotherDocPath) .toDispatch(); Dispatch tables = Dispatch.get(doc2, "Tables").toDispatch(); Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch(); Dispatch range = Dispatch.get(table, "Range").toDispatch(); Dispatch.call(range, "Copy"); if (this.find(pos)) { getRange(); Dispatch.call(this.range, "Paste"); } } catch (Exception e) { e.printStackTrace(); } finally { if (doc2 != null) { Dispatch.call(doc2, "Close", new Variant(true)); doc2 = null; } } } /** * 在当前文档指定的位置拷贝来自另一个文档中的图片 * * @param anotherDocPath * 另一个文档的磁盘路径 * @param shapeIndex * 被拷贝的图片在另一格文档中的位置 * @param pos * 当前文档指定的位置 */ public void copyImageFromAnotherDoc(String anotherDocPath, int shapeIndex, String pos) { Dispatch doc2 = null; try { doc2 = Dispatch.call(documents, "Open", anotherDocPath) .toDispatch(); Dispatch shapes = Dispatch.get(doc2, "InLineShapes").toDispatch(); Dispatch shape = Dispatch.call(shapes, "Item", new Variant(shapeIndex)).toDispatch(); Dispatch imageRange = Dispatch.get(shape, "Range").toDispatch(); Dispatch.call(imageRange, "Copy"); if (this.find(pos)) { getRange(); Dispatch.call(this.range, "Paste"); } } catch (Exception e) { e.printStackTrace(); } finally { if (doc2 != null) { Dispatch.call(doc2, "Close", new Variant(true)); doc2 = null; } } } /** * 在指定的表格的指定行前面增加行 * * @param tableIndex * word文件中的第N张表(从1开始) * @param rowIndex * 指定行的序号(从1开始) */ public void addTableRow(int tableIndex, int rowIndex) { getTable(tableIndex); getTableRows(); getTableRow(rowIndex); Dispatch.call(this.rows, "Add", new Variant(this.row)); } /** * 在第1行前增加一行 * * @param tableIndex * word文档中的第N张表(从1开始) */ public void addFirstTableRow(int tableIndex) { getTable(tableIndex); getTableRows(); Dispatch row = Dispatch.get(rows, "First").toDispatch(); Dispatch.call(this.rows, "Add", new Variant(row)); } /** * 在最后1行前增加一行 * * @param tableIndex * word文档中的第N张表(从1开始) */ public void addLastTableRow(int tableIndex) { getTable(tableIndex); getTableRows(); Dispatch row = Dispatch.get(this.rows, "Last").toDispatch(); Dispatch.call(this.rows, "Add", new Variant(row)); } /** * 增加一行 * * @param tableIndex * word文档中的第N张表(从1开始) */ public void addRow(int tableIndex) { getTable(tableIndex); getTableRows(); Dispatch.call(this.rows, "Add"); } /** * 增加一列 * * @param tableIndex * word文档中的第N张表(从1开始) */ public void addCol(int tableIndex) { getTable(tableIndex); getTableColumns(); Dispatch.call(this.cols, "Add").toDispatch(); Dispatch.call(this.cols, "AutoFit"); } /** * 在指定列前面增加表格的列 * * @param tableIndex * word文档中的第N张表(从1开始) * @param colIndex * 指定列的序号 (从1开始) */ public void addTableCol(int tableIndex, int colIndex) { getTable(tableIndex); getTableColumns(); getTableColumn(colIndex); Dispatch.call(this.cols, "Add", this.col).toDispatch(); Dispatch.call(this.cols, "AutoFit"); } /** * 在第1列前增加一列 * * @param tableIndex * word文档中的第N张表(从1开始) */ public void addFirstTableCol(int tableIndex) { getTable(tableIndex); Dispatch cols = getTableColumns(); Dispatch col = Dispatch.get(cols, "First").toDispatch(); Dispatch.call(cols, "Add", col).toDispatch(); Dispatch.call(cols, "AutoFit"); } /** * 在最后一列前增加一列 * * @param tableIndex * word文档中的第N张表(从1开始) */ public void addLastTableCol(int tableIndex) { getTable(tableIndex); Dispatch cols = getTableColumns(); Dispatch col = Dispatch.get(cols, "Last").toDispatch(); Dispatch.call(cols, "Add", col).toDispatch(); Dispatch.call(cols, "AutoFit"); } /** * 获取当前表格的列数 * * @return 列总数 */ public int getTableColumnsCount() { if (this.table == null) return 0; return Dispatch.get(this.cols, "Count").getInt(); } /** * 获取当前表格的行数 * * @return 行总数 */ public int getTableRowsCount() { if (this.table == null) return 0; return Dispatch.get(this.rows, "Count").getInt(); } /** * 获取当前表格的所有列对象 * * @return cols */ public Dispatch getTableColumns() { if (this.table == null) return this.cols; this.cols = Dispatch.get(this.table, "Columns").toDispatch(); return this.cols; } /** * 获取当前表格的所有行对象 * * @return rows */ public Dispatch getTableRows() { if (this.table == null) return this.rows; this.rows = Dispatch.get(this.table, "Rows").toDispatch(); return this.rows; } /** * 根据索引获得当前表格的列对象 * * @param columnIndex * 列索引 * @return col */ public Dispatch getTableColumn(int columnIndex) { if (this.cols == null) this.getTableColumns(); if (columnIndex >= 0) this.col = Dispatch.call(this.cols, "Item", new Variant(columnIndex)).toDispatch(); return this.col; } /** * 根据索引获得当前表格的行对象 * * @param rowIndex * 行索引 * @return row */ public Dispatch getTableRow(int rowIndex) { if (this.rows == null) this.getTableRows(); if (rowIndex >= 0) this.row = Dispatch.call(this.rows, "Item", new Variant(rowIndex)) .toDispatch(); return this.row; } /** * 自动调整当前所有表格 */ public void autoFitTable() { int count = this.getTablesCount(); for (int i = 0; i < count; i++) { Dispatch table = Dispatch.call(tables, "Item", new Variant(i + 1)) .toDispatch(); Dispatch cols = Dispatch.get(table, "Columns").toDispatch(); Dispatch.call(cols, "AutoFit"); } } /** * 根据行索引与列索引获取当前表格中的单元格 * * @param cellRowIdx * 行索引 * @param cellColIdx * 列索引 * @return cell对象 */ public Dispatch getCell(int cellRowIdx, int cellColIdx) { if (this.table == null) return this.cell; if (cellRowIdx >= 0 && cellColIdx >= 0) this.cell = Dispatch.call(this.table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)) .toDispatch(); return this.cell; } public void selectCell(int cellRowIdx, int cellColIdx) { if (this.table == null) return; getCell(cellRowIdx, cellColIdx); if (cellRowIdx >= 0 && cellColIdx >= 0) Dispatch.call(this.cell, "select"); } /** * 设置当前文档的标题 * * @param title 标题 * @param alignmentType 对齐方式 * @see setAlignment */ public void setTitle(String title, int alignmentType) { if (title == null || "".equals(title)) return; if (this.alignment == null) this.getAlignment(); if(alignmentType != 0 && alignmentType != 1 && alignmentType != 2) alignmentType = 0; Dispatch.put(this.alignment, "Alignment", alignmentType); Dispatch.call(this.selection, "TypeText", title); } /** * 设置当前表格边框的粗细 * * @param width * 范围:1 < w < 13, 如果是0,就代表沒有框<br/> */ public void setTableBorderWidth(int width) { if (this.table == null) return; /* * 设置表格线的粗细 1:代表最上边一条线 2:代表最左边一条线 3:最下边一条线 4:最右边一条线 5:除最上边最下边之外的所有横线 * 6:除最左边最右边之外的所有竖线 7:从左上角到右下角的斜线 8:从左下角到右上角的斜线 */ Dispatch borders = Dispatch.get(table, "Borders").toDispatch(); Dispatch border = null; for (int i = 1; i < 7; i++) { border = Dispatch.call(borders, "Item", new Variant(i)) .toDispatch(); if (width != 0) { Dispatch.put(border, "LineWidth", new Variant(width)); Dispatch.put(border, "Visible", new Variant(true)); } else if (width == 0) { Dispatch.put(border, "Visible", new Variant(false)); } } } /** * 得到指定的表格指定的单元格中的值 * * @param tableIndex * 表格索引(从1开始) * @param rowIndex * 行索引(从1开始) * @param colIndex * 列索引(从1开始) * @return */ public String getTxtFromCell(int tableIndex, int rowIndex, int colIndex) { String value = ""; // 设置为当前表格 getTable(tableIndex); getCell(rowIndex, colIndex); if (cell != null) { Dispatch.call(cell, "Select"); value = Dispatch.get(selection, "Text").toString(); value = value.substring(0, value.length() - 2); // 去掉最后的回车符; } return value; } /** * 对当前选中的内容设置项目符号与列表 * * @param tabIndex * <ul> * <li>1.项目编号</li> * <li>2.编号</li> * <li>3.多级编号</li> * <li>4.列表样式</li> * </ul> * @param index * 0表示没有,其它数字代表是该tab页中的第几项内容 */ public void applyListTemplate(int tabIndex, int index) { // 取得ListGalleries对象列表 Dispatch listGalleries = Dispatch.get(this.word, "ListGalleries") .toDispatch(); // 取得列表中一个对象 Dispatch listGallery = Dispatch.call(listGalleries, "Item", new Variant(tabIndex)).toDispatch(); Dispatch listTemplates = Dispatch.get(listGallery, "ListTemplates") .toDispatch(); if (this.range == null) this.getRange(); Dispatch listFormat = Dispatch.get(this.range, "ListFormat") .toDispatch();
复制代码
作者:
遗矢的幸福
时间:
2011-12-20 16:19
好乱啊
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2