📄 mswordmailmerger.java
字号:
/*
* Copyright (c) 2007 Software Wizards Pty Ltd, Victoria, Australia.
* mailto:enquires@swz.com.au. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted. See the GNU General Public License
* for more details.
*
* Redistribution of the SWTtoCOM software is not permitted as part of any
* commercial product. Licensing terms for such distribution may be
* obtained from the copyright holder.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package au.com.swz.swttocom.example.msword;
import java.util.Enumeration;
import au.com.swz.swttocom.example.msword.wordom.Application;
import au.com.swz.swttocom.example.msword.wordom.Cell;
import au.com.swz.swttocom.example.msword.wordom.Columns;
import au.com.swz.swttocom.example.msword.wordom.Document;
import au.com.swz.swttocom.example.msword.wordom.MailMergeField;
import au.com.swz.swttocom.example.msword.wordom.MailMergeFields;
import au.com.swz.swttocom.example.msword.wordom.Range;
import au.com.swz.swttocom.example.msword.wordom.Row;
import au.com.swz.swttocom.example.msword.wordom.Rows;
import au.com.swz.swttocom.example.msword.wordom.Table;
import au.com.swz.swttocom.example.msword.wordom.Tables;
public class MSWordMailMerger {
private Document document;
public MSWordMailMerger(Document document) {
this.document = document;
}
public void merge(IMailMergeDataSource dataSource) {
Application application = document.getApplication();
MailMergeFields fields = document.getMailMerge().getFields();
Tables tables = document.getTables();
int tableCount = tables.getCount();
for (int i=1; i<=tableCount; i++ )
{
Table table = tables.item(i);
Columns columns = table.getColumns();
if ((columns == null) || columns.getCount() <= 0) {
continue;
}
if (table.getRows().getCount() <= 0) {
continue;
}
Range tableRange = table.getRange();
for (Enumeration fldEnum=fields.elements(); fldEnum.hasMoreElements(); )
{
MailMergeField field = (MailMergeField) fldEnum.nextElement();
if (field.getCode().inRange(tableRange)) {
processTable(table, columns, fields, dataSource);
break;
}
}
}
while (fields.getCount() > 0) {
MailMergeField field = fields.item(1);
if (field == null) {
return;
}
String fieldContents = dataSource.getFieldText(field.getCode().getText());
if (fieldContents == null) {
fieldContents = "Unknown Field Code";
}
field.select();
Range fieldRange = application.getSelection().getRange();
fieldRange.setText(fieldContents);
}
}
public void processTable(
Table table,
Columns columns,
MailMergeFields fields,
IMailMergeDataSource dataSource)
{
int columnCount = columns.getCount();
String[] fieldArray = new String[columnCount];
for (int col=0; col<columnCount; col++ ) {
Cell cell = table.cell(2, col+1);
Range cellRange = cell.getRange();
fieldArray[col] = null;
for (Enumeration fldEnum=fields.elements(); fldEnum.hasMoreElements(); )
{
MailMergeField field = (MailMergeField) fldEnum.nextElement();
Range fieldRange = field.getCode();
if (fieldRange.inRange(cellRange)) {
fieldArray[col] = field.getCode().getText();
break;
}
}
}
int row = 1;
Rows rows = table.getRows();
Row firstRow = rows.item(2);
while (true) {
boolean rowInserted = false;
for (int col=0; col<columnCount; col++ ) {
if (fieldArray[col] == null) {
continue;
}
String fieldContents = dataSource.getTableFieldText(row, fieldArray[col]);
if (fieldContents == null) {
firstRow.delete();
return;
}
if (!rowInserted) {
rows.add(firstRow.createSWTVariant());
rowInserted = true;
}
Cell cell = table.cell(row+1, col+1);
cell.getRange().setText(fieldContents);
}
row++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -