1

我有一个包含多个 Word 表格的文档。我需要将它们转换为嵌入式 Excel 工作表(或 COM 对象)。我已经能够使用以下命令将 Word 表格“导入”到 Excel 中:

        Excel.Application xlApp = new Excel.Application();
        // Call the conversion tool
        for (int i = 1; i <= curDoc.Tables.Count; i++ )
        {
            Word.Table tbl = curDoc.Tables[i];
            Word.Range tblLoc = tbl.Range;



            // Used for debugging.
            xlApp.Visible = true;

            if (xlApp == null)
            {
                messageAlert = "Excel could not be started.  Check that your office installation and project references are correct.";
                break;
            }

            Excel.Workbook wb = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];


            if (ws == null)
            {
                messageAlert = "Worksheet could not be created.  Check that your office installation and project reference are correct.";
                break;
            }

            Word.Range rng = tbl.ConvertToText(Separator: ";", NestedTables: false);

            string sData = rng.Text;



            string[] rows = sData.Split('\r');

            int r = 1, c = 1;
            int numRows = rows.Count();
            int numCols = rows[0].Split(';').Count();


            foreach (string row in rows)
            {
                string[] cells = row.Split(';');
                foreach (string cell in cells)
                {
                    ws.Cells[r, c].Value = cell;
                    c += 1;
                }
                r += 1;
                c = 1;
            }

问题是每当我将内容复制回文档时,都会创建一个新的 Word 表格而不是 Excel 工作表。如何将 Excel 工作表导入 Word,或直接将表格转换为 Excel 工作表?

4

1 回答 1

1

为此,您必须先保存 excel 工作表,然后将其导入为OLEObject. 这是一个例子:

public void ConvertTables()
{
    string messageAlert = "";
    Word.Application curApp = Globals.ThisAddIn.Application;

    Word.Document curDoc = curApp.ActiveDocument;
    if (curDoc.Tables.Count > 0)
    {
        Excel.Application xlApp = new Excel.Application();

        //Used for debugging.
        //xlApp.Visible = true;

        //Call the conversion tool
        for (int i = 1; i <= curDoc.Tables.Count; i++ )
        {
            Word.Table tbl = curDoc.Tables[i];
            Word.Range tblLoc = tbl.Range;

            if (xlApp == null)
            {
                messageAlert = "Excel could not be started.  Check that your office installation and project references are correct.";
                break;
            }

            Excel.Workbook wb = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];


            if (ws == null)
            {
                messageAlert = "Worksheet could not be created.  Check that your office installation and project reference are correct.";
                break;
            }

            Word.Range rng = tbl.ConvertToText(Separator: ";", NestedTables: false);

            string sData = rng.Text;



            string[] rows = sData.Split('\r');

            int r = 1, c = 1;
            int numRows = rows.Count();
            int numCols = rows[0].Split(';').Count();


            foreach (string row in rows)
            {
                string[] cells = row.Split(';');
                foreach (string cell in cells)
                {
                    ws.Cells[r, c].Value = cell;
                    c += 1;
                }
                r += 1;
                c = 1;
            }

            ws.SaveAs("C:\\temp\\test.xlsx");
            rng.Text = "";
            rng.InlineShapes.AddOLEObject(ClassType: "Excel.Sheet.12", FileName: "C:\\temp\\test.xlsx");


            ws.Range["A1", ws.Cells[numRows, numCols]].Value = "";
            ws.SaveAs("C:\\Temp\\test.xlsx");
        }
        xlApp.Quit();

        messageAlert = "Tables converted";
    }
    else
    {
        // No tables found
        messageAlert = "No tables found within the document";
    }


    MessageBox.Show(messageAlert);

}
于 2013-09-19T19:56:06.747 回答