簡體   English   中英

需要從單詞表中獲取項目符號才能使用互操作性來表現出色

[英]Need to get bullets from word tables to excel using interop

因此,我一直在嘗試學習如何使用Interop(對此通常進行編程)。 基本上,我的Word文檔中有這些表,我需要將這些表導出到Excel。

我已經獲得了要導出到Excel的基本文本,但是我在Word中的表具有相當多的格式設置,主要是在將某些單元格放入excel后需要保留或重建的某些單元格中的項目符號列表?

這是我的代碼。 顯然這將導出文本。 通過一些研究,我能夠獲得項目符號和縮進,並在一個富文本框中看到它們(請參見注釋的代碼)。 我需要這個來一個接一個地工作。 我無法在數組中構建所有內容並將其傳遞給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”解決方案。 基本上,我創建了一個自定義類並使用了它,而不是嘗試通過互操作來完成整個工作,而我發現這樣做很局限。 最后有一點作弊,我在項目符號中使用了“ *”和“-”。 有一種方法可以使用某種應用程序發送密鑰方法實際將密鑰發送給excel( https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._application.sendkeys ( v=office .11).aspx ),但我今天沒有時間。

我在代碼中添加了注釋以提供幫助。 希望有人覺得這有用。

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


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM