如何将Word表格转换为嵌入式Excel工作表?

时间:2013-09-19 17:51:03

标签: visual-studio-2010 c#-4.0 vsto excel-2010 word-2010

我有一个包含多个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工作表?

1 个答案:

答案 0 :(得分: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);

}