首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >需要使用interop从word表中获取符号以进行excel。

需要使用interop从word表中获取符号以进行excel。
EN

Stack Overflow用户
提问于 2015-12-10 16:35:33
回答 1查看 404关注 0票数 1

因此,我一直试图学习如何使用Interop (以及一般情况下的编程)。基本上,我的Word文档中有这些表,需要导出到Excel中。

我已经获得了导出到excel的基本文本,但是Word中的表有相当数量的格式,主要是在一些单元格中的项目列表,一旦我将它们放入Excel,就可能需要保留或重建这些列表??

这是我的密码。这就明显地导出了文本。通过一些研究,我能够获得子弹和缩进,并在一个丰富的文本框中看到它们(参见注释代码)。我需要这个来一个接一个的工作。我不能在数组中构建所有东西并将其传递给Excel。

这是我试图更新的应用程序中的一个限制。这里的代码只是一个测试用例。我的问题是,如何使它在excel中导出或重建?

我找到了一些关于这个问题的文章,但是没有任何东西能帮助我度过这个难关,这就是我为什么在这里问的原因。任何帮助都将不胜感激。

代码语言:javascript
运行
复制
private void btnExecute_Click(object sender, EventArgs e)
{
    var word = new Microsoft.Office.Interop.Word.Application();

    Excel.Application excelApp = new Excel.Application();
    Excel.Workbook excelworkbook = excelApp.Workbooks.Open("D:\\mytest.xlsx");
    Excel._Worksheet excelworkSheet = (Excel.Worksheet)excelApp.ActiveSheet;

    excelApp.Visible = true;

    object miss = System.Reflection.Missing.Value;
    object path = txbxFilePath1.Text;
    object readOnly = true;
    var docs = word.Documents.Open(ref path, ref miss, ref readOnly,
                               ref miss, ref miss, ref miss, ref miss,
                               ref miss, ref miss, ref miss, ref miss,
                               ref miss, ref miss, ref miss, ref miss,
                               ref miss);

    foreach (Table tb in docs.Tables)
    {
        for (int row = 1; row <= tb.Rows.Count; row++)
        {
            for (int mycols = 1; mycols <= tb.Columns.Count; mycols++)
            {
                var cells = tb.Cell(row, mycols).Range.Paragraphs;

                foreach (Paragraph para in cells)
                {
                    //string bulltxt = para.Range.Text;
                    //string leftIndent = para.LeftIndent.ToString();
                    //string bulletStr = para.Range.ListFormat.ListString;
                    //rTxBxResult.AppendText(leftIndent + "\t" + bulletStr + "\t" + para.Range.Text);
                }

                excelworkSheet.Cells[row, mycols] = tb.Cell(row, mycols).Range.Text;
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-10 22:44:33

因此,在一个朋友的帮助下,我们想出了一个“解决方案”。基本上,我已经创建了一个自定义类,并使用它,而不是试图通过互操作( interop )来完成整个工作,这是我发现的限制。在结尾我用“*”和“-”来形容我的子弹人物。有一种使用某种应用程序发送密钥到excel的方法(application.sendkeys(v=office.11).aspx,但我今天没有时间。)

我在代码中添加了注释以提供帮助。希望有人能找到有用的东西。

代码语言:javascript
运行
复制
    private void btnExecute_Click(object sender, EventArgs e)
    {

        var word = new Microsoft.Office.Interop.Word.Application();

        Excel.Application excelApp = new Excel.Application();
        Excel.Workbook excelworkbook = excelApp.Workbooks.Open("D:\\myxlspreadsheet");
        Excel._Worksheet excelworkSheet = (Excel.Worksheet)excelApp.ActiveSheet;



        excelApp.Visible = true;

        object miss = System.Reflection.Missing.Value;
        object path = txbxFilePath1.Text;
        object readOnly = true;
        var docs = word.Documents.Open(ref path, ref miss, ref readOnly, 
                                   ref miss, ref miss, ref miss, ref miss, 
                                   ref miss, ref miss, ref miss, ref miss, 
                                   ref miss, ref miss, ref miss, ref miss, 
                                   ref miss);

        //Get the tables in the word Document
        foreach (Table tb in docs.Tables)
        {                
            for (int row = 1; row <= tb.Rows.Count; row++)
            {
                for (int mycols = 1; mycols <= tb.Columns.Count; mycols++)
                {
                    var cells = tb.Cell(row, mycols).Range.Paragraphs;

                    //Create a List using your custom formatter Class
                    List<Formatter> lFormatter = new List<Formatter>();                   
                    foreach (Paragraph para in cells)
                    {
                        Formatter formatter = new Formatter();
                        formatter.strCellText = para.Range.Text;
                        formatter.flIndent = para.LeftIndent;
                        formatter.strBullet = para.Range.ListFormat.ListString;
                        rTxBxResult.AppendText(formatter.strCombine);
                        lFormatter.Add(formatter);
                    }
                    for (int i =0; i< lFormatter.Count; i++)
                    {
                        Formatter formatter = lFormatter[i];
                        if(i==0)
                            excelworkSheet.Cells[row, mycols] = ((string)(excelworkSheet.Cells[row, mycols] as Excel.Range).Value2) + formatter.strCombine;
                        else
                            excelworkSheet.Cells[row, mycols] = ((string)(excelworkSheet.Cells[row, mycols] as Excel.Range).Value2) + Environment.NewLine + formatter.strCombine;
                    }

                }                    
            }
        }
    }

}

//Use this class to store the collected values from the rows and colums of the word table
public class Formatter
{
    public string strCellText;
    public string strIndent = "";
    public float flIndent;
    public string strBullet;
    //Combine the pieces together and manipulate the strings as needed
    public string strCombine
    {
        get 
        {
            //first indent is 36 so 1 tab, second indent is 72 so 2 tabs, etc
            //alternate * and dashes for bullet marks in excel using odd and even numbers
            decimal newIndent = Math.Round((decimal)(flIndent / 36));
            for (int i = 0; i < newIndent; i++)
            {
                strIndent = strIndent + "  ";
            }
            if (newIndent == 0)
                strBullet = "";
            else if (IsOdd((int)newIndent))
                strBullet="*";
            else
                strBullet = "-";
            return strIndent + strBullet + strCellText;                
        }
    }

    public static bool IsOdd(int value)
    {
    return value % 2 != 0;
    }


}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34207102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档