Domanda

I have a document that has multiple Word Tables. I need to convert them into embedded Excel Worksheets (or COM Objects). I've been able to "import" the Word Tables into Excel using the following:

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

Problem is whenever I copy the contents back into the document, a new Word Table is created instead of an Excel Worksheet. How do I either import an Excel Worksheet into Word, or directly convert the tables into Excel Worksheets?

È stato utile?

Soluzione

In order to do this, you'll have to first save the excel worksheet and then import it as an OLEObject. Here's an example:

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

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top