| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 655 人关注过本帖
标题:有哪位高手指导一下怎么从word的表格里把想要的某列读取到excel中
只看楼主 加入收藏
张京奎
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2012-11-10
结帖率:60%
收藏
已结贴  问题点数:20 回复次数:3 
有哪位高手指导一下怎么从word的表格里把想要的某列读取到excel中
有哪位高手指导一下怎么从word的表格里把想要的某列读取到excel中
搜索更多相关主题的帖子: excel 
2012-11-10 20:52
青春无限
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江苏
等 级:贵宾
威 望:24
帖 子:3451
专家分:19340
注 册:2012-3-31
收藏
得分:0 
看看

学 会看代码…学习写程序…学会搞开发…我的目标!呵呵是不是说大话啊!!一切皆可能
2012-11-11 09:15
mmxo
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:13
帖 子:189
专家分:1090
注 册:2012-11-7
收藏
得分:20 
两个按钮两个ListBox,按钮ButOpenWordDocument用于打开*.doc或*.docx文档,打开文档时查找所有表格并设置表格的ID,如果你不想改变文档的数据的话,就把文表格搞个字典来分辨吧,这里为示例简便而没有采用这种方法;按钮ButExportColumnsToExcel用于将LbColumns中选定的列导入到Excel中;列表框LbTableNames用户显示文档中的表格,有Title的表格显示Title,没有的显示“表格#+Index”,选定其中一个表格,则在LbColumns中列出表格中的列,格式为“列+索引”;列表框LbColumns中可以选定列,选定的列中的文本将被导出到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;
    }
}


 

为提高中华编程水平而奋斗
2012-11-11 12:25
张京奎
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2012-11-10
收藏
得分:0 
回复 3楼 mmxo
谢谢啦!
2012-11-12 20:53
快速回复:有哪位高手指导一下怎么从word的表格里把想要的某列读取到excel中
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034584 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved