有哪位高手指导一下怎么从word的表格里把想要的某列读取到excel中
有哪位高手指导一下怎么从word的表格里把想要的某列读取到excel中
出于对COM组件的特殊信任感……,还是把测试环境列一下:Win7、Word2010、Excel2010、VS2012、NF4.5
程序代码://FormMain.cs
using System;
using System.Globalization;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Word;
using Range = Microsoft.Office.Interop.Excel.Range;
namespace ReadWordGridToExcel
{
public partial class FormMain : Form
{
#region 常量
private const string Filter = "Word文件(*.doc)|*.doc|Word文件(*.docx)|*.docx";
#endregion
#region 全局字段
private Microsoft.Office.Interop.Excel.Application _excelApplication;
private Microsoft.Office.Interop.Word.Application _wordApplication;
private Document _wordDocument;
#endregion
#region 构造函数
public FormMain()
{
InitializeComponent();
LbTableNames.SelectedIndexChanged += LbTableNames_SelectedIndexChanged;
}
#endregion
#region 控件事件
private void ButExportColumnsToExcel_Click(object sender, EventArgs e)
{
if (LbColumns.SelectedIndices.Count == 0) return;
if (_excelApplication == null) _excelApplication = new Microsoft.Office.Interop.Excel.Application { Visible = true };
var workBook = _excelApplication.Workbooks.Add();
var excelSheetEnumerator = workBook.Worksheets.GetEnumerator();
Worksheet excelSheet = null;
while (excelSheetEnumerator.MoveNext())
{
excelSheet = (Worksheet)excelSheetEnumerator.Current;
break;
}
if (excelSheet == null) return;
var wordTableEnumerator = _wordDocument.Tables.GetEnumerator();
Table wordTable = null;
while (wordTableEnumerator.MoveNext())
{
var currentWordTable = (Table) wordTableEnumerator.Current;
if (currentWordTable.ID != LbTableNames.SelectedIndex.ToString(CultureInfo.InvariantCulture)) continue;
wordTable = currentWordTable;
break;
}
if (wordTable == null) return;
var wordColumns = wordTable.Columns;
var excelColumns = excelSheet.Columns;
var wordTableColumnIndex = 0;
foreach (int index in LbColumns.SelectedIndices)
{
var wordColumnEnumerator = wordColumns.GetEnumerator();
Column wordColumn = null;
while (wordColumnEnumerator.MoveNext())
{
var currentWordColumn = (Column) wordColumnEnumerator.Current;
if (currentWordColumn.Index != index + 1) continue;
wordColumn = currentWordColumn;
break;
}
if (wordColumn == null) continue;
var excelColumnEnumerator = excelColumns.GetEnumerator();
Range excelColumn = null;
var excelColumnIndex = 0;
while (excelColumnEnumerator.MoveNext())
{
if (excelColumnIndex != wordTableColumnIndex)
{
excelColumnIndex++;
continue;
}
excelColumn = (Range)excelColumnEnumerator.Current;
break;
}
if (excelColumn == null) return;
var excelColumnCells = excelColumn.Cells;
var excelColumnCellEnumerator = excelColumnCells.GetEnumerator();
var wordColumnCellEnumerator = wordColumn.Cells.GetEnumerator();
while (wordColumnCellEnumerator.MoveNext())
{
var currentWordColumnCell = (Cell)wordColumnCellEnumerator.Current;
var text = currentWordColumnCell.Range.Text.TrimEnd("\r\a".ToCharArray());
excelColumnCellEnumerator.MoveNext();
var excelCell = (Range) excelColumnCellEnumerator.Current;
excelCell.Value = text;
}
wordTableColumnIndex++;
}
}
private void ButOpenWordDocument_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog {Filter = Filter};
if (ofd.ShowDialog() != DialogResult.OK)
return;
if (_wordApplication == null)
_wordApplication = new Microsoft.Office.Interop.Word.Application {Visible = true};
_wordDocument = _wordApplication.Documents.Open(ofd.FileName);
LbTableNames.Items.Clear();
var tableIndex = 0;
foreach (Table table in _wordDocument.Tables)
{
table.ID = tableIndex.ToString(CultureInfo.InvariantCulture);
LbTableNames.Items.Add(table.Title ?? string.Concat("表格#", tableIndex));
tableIndex++;
}
}
void LbTableNames_SelectedIndexChanged(object sender, EventArgs e)
{
var enumerator = _wordDocument.Tables.GetEnumerator();
LbColumns.Items.Clear();
while (enumerator.MoveNext())
{
var table = (Table)enumerator.Current;
if (table.ID != LbTableNames.SelectedIndex.ToString(CultureInfo.InvariantCulture)) continue;
foreach (Column column in table.Columns)
LbColumns.Items.Add(string.Concat("列", column.Index));
}
}
#endregion
}
}
//FormMain.Designer.csnamespace ReadWordGridToExcel
{
partial class FormMain
{
private components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.ButOpenWordDocument = new System.Windows.Forms.Button();
this.LbTableNames = new System.Windows.Forms.ListBox();
this.LbColumns = new System.Windows.Forms.ListBox();
this.ButExportColumnsToExcel = new System.Windows.Forms.Button();
this.SuspendLayout();
this.ButOpenWordDocument.Location = new System.Drawing.Point(12, 12);
this.ButOpenWordDocument.Name = "ButOpenWordDocument";
this.ButOpenWordDocument.Size = new System.Drawing.Size(153, 23);
this.ButOpenWordDocument.TabIndex = 0;
this.ButOpenWordDocument.Text = "Open Word Document...";
this.ButOpenWordDocument.UseVisualStyleBackColor = true;
this.ButOpenWordDocument.Click += new System.EventHandler(this.ButOpenWordDocument_Click);
this.LbTableNames.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left)));
this.LbTableNames.FormattingEnabled = true;
this.LbTableNames.ItemHeight = 12;
this.LbTableNames.Location = new System.Drawing.Point(12, 41);
this.LbTableNames.Name = "LbTableNames";
this.LbTableNames.Size = new System.Drawing.Size(153, 148);
this.LbTableNames.TabIndex = 1;
this.LbColumns.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) |
System.Windows.Forms.AnchorStyles.Right)));
this.LbColumns.FormattingEnabled = true;
this.LbColumns.ItemHeight = 12;
this.LbColumns.Location = new System.Drawing.Point(171, 41);
this.LbColumns.Name = "LbColumns";
this.LbColumns.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
this.LbColumns.Size = new System.Drawing.Size(263, 148);
this.LbColumns.TabIndex = 2;
this.ButExportColumnsToExcel.Anchor =
((System.Windows.Forms.AnchorStyles)
(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.ButExportColumnsToExcel.Location = new System.Drawing.Point(171, 12);
this.ButExportColumnsToExcel.Name = "ButExportColumnsToExcel";
this.ButExportColumnsToExcel.Size = new System.Drawing.Size(263, 23);
this.ButExportColumnsToExcel.TabIndex = 3;
this.ButExportColumnsToExcel.Text = "Export Selected Columns To Excel...";
this.ButExportColumnsToExcel.UseVisualStyleBackColor = true;
this.ButExportColumnsToExcel.Click += new System.EventHandler(this.ButExportColumnsToExcel_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(446, 205);
this.Controls.Add(this.ButExportColumnsToExcel);
this.Controls.Add(this.LbColumns);
this.Controls.Add(this.LbTableNames);
this.Controls.Add(this.ButOpenWordDocument);
this.Name = "FormMain";
this.ShowIcon = false;
this.Text = "Read Word Grid To Excel by ";
this.ResumeLayout(false);
}
private System.Windows.Forms.Button ButOpenWordDocument;
private System.Windows.Forms.ListBox LbTableNames;
private System.Windows.Forms.ListBox LbColumns;
private System.Windows.Forms.Button ButExportColumnsToExcel;
}
}
