需要使用互操作从word表中获取项目符号

时间:2015-12-10 16:35:33

标签: c# excel 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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

因此,在朋友的帮助下,我们提出了“解决方案”。基本上我已经创建了一个自定义类并使用它而不是尝试通过互操作完成整个事情,我发现它限制了这一点。最后有一点作弊,我用'*'和' - '作为我的子弹角色。有一种方法可以使用某种应用程序发送密钥方法(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;
    }


}