EXCEL:读取多个WORD文档文件到EXCEL同一工作表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Word = Microsoft.Office.Interop.Word;
using System.Threading;
using office = Microsoft.Office.Core;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            BindListBox();
            txtSavePath.Text = Server.MapPath("~/产品列表.xls");//初始化文件保存路径
        }
    }

    protected void btnRead_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();//实例化Word对象
        Microsoft.Office.Interop.Word.Table table;//声明Word表格对象
        int P_int_TableCount = 0, P_int_Row = 0, P_int_Column = 0;//定义3个变量,分别用来存储表格数量、表格中的行数、列数
        string P_str_Content;//存储Word表格的单元格内容
        object missing = System.Reflection.Missing.Value;//获取缺少的object类型值
        string[] P_str_Names = new string[ListBox1.Items.Count];
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            P_str_Names[i] = ListBox1.Items[i].Value;
        }
        object P_obj_Name;//存储遍历到的Word文件名
        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//实例化Excel对象
        //打开指定的Excel文件
        Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Open(txtSavePath.Text, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
        Microsoft.Office.Interop.Excel.Worksheet newWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(missing, missing, missing, missing);//创建新工作表
        for (int i = 0; i < P_str_Names.Length; i++)//遍历所有选择Word文件名
        {
            P_obj_Name = P_str_Names[i];//记录遍历到的Word文件名
            //打开Word文档
            Microsoft.Office.Interop.Word.Document document = word.Documents.Open(ref P_obj_Name, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            P_int_TableCount = document.Tables.Count;//获取Word文档中表格的数量
            if (P_int_TableCount > 0)//判断表格数量是否大于0
            {
                for (int j = 1; j <= P_int_TableCount; j++)//遍历所有表格
                {
                    table = document.Tables[j];//记录遍历到的表格
                    P_int_Row = table.Rows.Count;//获取表格行数
                    P_int_Column = table.Columns.Count;//获取表格列数
                    for (int row = 1; row <= P_int_Row; row++)//遍历表格中的行
                    {
                        for (int column = 1; column <= P_int_Column; column++)//遍历表格中的列
                        {
                            P_str_Content = table.Cell(row, column).Range.Text;//获取遍历到的单元格内容
                            newWorksheet.Cells[i + row, column] = P_str_Content.Substring(0, P_str_Content.Length - 2);//将遍历到的单元格内容添加到Excel单元格中
                        }
                    }
                }
            }
            else
            {
                if (P_int_Row == 0)//判断前面是否已经填充过表格
                    newWorksheet.Cells[i + P_int_Row + 1, 1] = document.Content.Text;//直接将Word文档内容添加到工作表中
                else
                    newWorksheet.Cells[i + P_int_Row, 1] = document.Content.Text;//直接将Word文档内容添加到工作表中
            }
            document.Close(ref missing, ref missing, ref missing);//关闭Word文档
        }
        excel.Application.DisplayAlerts = false;//不显示提示对话框
        workbook.Save();//保存工作表
        workbook.Close(false, missing, missing);//关闭工作表
        word.Quit(ref missing, ref missing, ref missing);//退出Word应用程序
        Response.Write("<script>alert('已经成功将多个Word文档的内容合并到了Excel的同一个数据表中!');</script>");
    }

    protected void btnBrowse_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start(txtSavePath.Text);//打开选择的Excel文件
    }

    private void BindListBox()
    { 
        ListBox1.Items.Clear();
        string path = Server.MapPath("~/File");
        string[] files = System.IO.Directory.GetFiles(path);
        foreach (string f in files)
        { 
            string fileName = System.IO.Path.GetFileName(f);
            ListItem li = new ListItem(fileName,f);
            ListBox1.Items.Add(li);
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值