当我将数据从html tabel放到excel时,如何在excel中设置rowspan / colspan? [英] How can I set rowspan/ colspan in an excel when I put data from html tabel to excel ?

查看:86
本文介绍了当我将数据从html tabel放到excel时,如何在excel中设置rowspan / colspan?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下引用将html表导出到excel。

使用Microsoft.Office.Interop.Excel;





这是我的代码:



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



通过此代码我可以将数据放入单元格。但是我可以使用rowspan或colspan来放置数据。

当我将数据放入excel时,如何将rowspan或colspan设置为excel?有可能吗?


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 ?

推荐答案

是的,确实如此。使用合并方法 [ ^ ]。 />


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





轻松合并Excel单元格在C#,VB.NET [ ^ ]


这篇关于当我将数据从html tabel放到excel时,如何在excel中设置rowspan / colspan?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆