sinat_30056991
sinat_30056991
采纳率61.5%
2015-08-28 08:15

用c#获取excel表格内容,转换成html源代码输出,怎么判断和输出合并单元格?

已采纳
excel = new Excel.Application();
                excel.UserControl = true;
                excel.DisplayAlerts = false;
                excel.Application.Workbooks.Open(fileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                wbs = excel.Workbooks;
                wb = wbs[1];
                for (int k = 0; k < wb.Worksheets.Count; k++)
                {
                    ws = (Excel.Worksheet)wb.Worksheets[k + 1];
                    if (ws == null) continue;
                    int rowCount = ws.UsedRange.Rows.Count;
                    int colCount = ws.UsedRange.Columns.Count;
                    sb.AppendLine("<table width=\"100%\" cellpadding=0 cellspacing=0 border=1 bordercolor=\"#888888\" style=\"border-collapse:collapse;padding:4px;\">");
                    for (int i = 0; i < rowCount; i++)
                    {
                        sb.AppendLine("<tr>");
                        for (int j = 0; j < colCount; j++)
                        {
                            range1 = ws.Range[ws.Cells[i + 1, j + 1], ws.Cells[i + 1, j + 1]];
                            if (range1 != null)
                            {
                                if (range1.Text.ToString() == "")
                                {
                                    sb.AppendLine("<td></td>");
                                }
                                else
                                {
                                 sb.AppendLine("<td>" + range1.Text + "</td>");
                                }
                            }
                            else
                            {
                                sb.AppendLine("");
                            }
                        }
                        sb.AppendLine("</tr>");
                    }
                    sb.AppendLine("</table>");
                    sb.AppendLine("<br>");

如题

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

相关推荐