Click here to Skip to main content
15,219,549 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am using following reference to export html table into excel.
using Microsoft.Office.Interop.Excel;


This is my code :

Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            workbook.Sheets.Add();

            Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null;
            worksheet1 = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets[1];

            var itemset1 = webBrowser2.Document.GetElementById(idList[0]);
            string str1 = "";
            int rowCount1 = itemset1.GetElementsByTagName("tr").Count;
            int colCount1 = itemset1.GetElementsByTagName("tr")[0].GetElementsByTagName("td").Count;

            for (int i = 0; i < rowCount1; i++)
            {
                for (var j = 0; j < colCount1; j++)
                {
                    if (itemset1.GetElementsByTagName("tr")[i].GetElementsByTagName("td")[j].InnerText == "th")
                    {
                        str1 = itemset1.GetElementsByTagName("tr")[i].GetElementsByTagName("th")[j].InnerText;
                    }

                    else
                    {
                        str1 = itemset1.GetElementsByTagName("tr")[i].GetElementsByTagName("td")[j].InnerText;
                    }
                    worksheet1.Cells[i + 1, j + 1] = str1;
                }
            }
string fileDestination = @"D:\test.xls";

            if (File.Exists(fileDestination))
            {
                File.Delete(fileDestination);
            }

            workbook.SaveAs(fileDestination, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            workbook.Close(true, Type.Missing, Type.Missing);
            Process.Start(fileDestination);
            app.Quit();

Through this code I can put data into cell. But I can put data with its rowspan or colspan.
How can I set rowspan or colspan to excel when I put data into excel ? is it possible ?
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Yes, it is. Use Merge method[^].

worksheet1.get_Range("A1:C55").Merge();
//or
worksheet1.get_Range(worksheet1.Cells[1,1],worksheet1.Cells[3,5]).Merge();
//or
oRng = ws.get_Range("A1", "B2");
oRng.Merge(Missing.Value);


Easily Merge Excel Cells in C#, VB.NET[^]
   
v2
Comments
sachi Dash 17-Oct-14 0:58am
   
can you please give me some details with an example where this method is implemented ?
Maciej Los 17-Oct-14 2:09am
   
Please, see updated answer ;)
sachi Dash 17-Oct-14 3:25am
   
Thanks a lot. Actually I already see it. Can you please tell me what is A1 or B2 and what do you mean by C55 ?
Maciej Los 17-Oct-14 3:28am
   
Cells[column:=1,row:=1] = A1
Cells[column:=3,row:=5] = C5
and so on...
It means the same, but use different addressing type.
Can you accept my answer as a solution (green button) - formally to remove your question from unanswered list.
sachi Dash 18-Oct-14 2:20am
   
worksheet1.Range[worksheet1.Cells[i + 1, j + 1], worksheet1.Cells[i+1,j+colSpan]].Merge();

Now I am using in this way... I am using Range instead of get_Range. ... Thanks a lot again !
Maciej Los 18-Oct-14 10:48am
   
You're very welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100