需要使用互操作从单词表中获取项目符号到 excel

Need to get bullets from word tables to excel using interop

所以我一直在尝试学习如何使用 Interop(以及与此相关的一般编程)。基本上我的 Word 文档中有这些表格,我需要将其导出到 Excel。

我已经得到要导出到 Excel 的基本文本,但是我在 Word 中的表格有相当多的格式,主要是一些我需要保留或重建一次的单元格中的项目符号列表我让他们进入 excel?

这是我的代码。这显然会导出文本。通过一些研究,我能够获得项目符号和缩进,并在 richtextbox 中看到它们(参见注释代码)。我需要这个来逐个单元地工作。我无法在数组中构建所有内容并将其传递给 Excel。

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

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

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;
            }
        }
    }
}

因此,在朋友的帮助下,我们想出了 'a' 解决方案。基本上我已经创建了一个自定义 class 并使用它而不是尝试通过互操作来完成整个事情,我发现它对此有限制。最后有一个小作弊,我使用'*'和'-'作为我的子弹字符。有一种方法可以使用某种应用程序发送密钥方法 (https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._application.sendkeys(v=office.11).aspx) 将密钥实际发送到 excel,但我今天没有时间。

我在代码中添加了注释以提供帮助。希望有人觉得这很有用。

    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;
    }


}