我在C#中做了这一切
我创建了一个如下表
Company Name Gender
cmc sindhu f
cmc shilpa f
cmc srikar m
但我需要的是......我希望“cmc”只对所有名字出现一次。
Company Name Gender
cmc sindhu f
shilpa f
srikar m
如何使用rowspan
和colspan
?我的代码如下:
public class sample
{
static void Main(string[] args)
{
DataTable table=new DataTable();
table.Columns.Add("Company", typeof(string));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Gender", typeof(string));
table.Rows.Add("cmc", "sindhu", "f");
table.Rows.Add("cmc", "shilpa", "f");
table.Rows.Add("cmc", "srikar", "m");
string htmltable;
string htmlstring;
htmltable=ConvertDataTableToHtml(table);
string filename = "HTML File.html";
StreamWriter swXLS = new StreamWriter( "c:\\"+ filename);
swXLS.Write(htmltable.ToString());
swXLS.Close();
}
public static string ConvertDataTableToHtml(DataTable targetTable)
{
string htmlString = "";
if (targetTable == null)
{
throw new System.ArgumentNullException("targetTable");
}
StringBuilder htmlBuilder = new StringBuilder();
//Create Top Portion of HTML Document
htmlBuilder.AppendLine("<html>");
htmlBuilder.AppendLine("<head>");
htmlBuilder.AppendLine("</head>");
htmlBuilder.AppendLine("<body>");
htmlBuilder.AppendLine("<table border='1px' cellpadding='5' cellspacing='0'>");
//Create Header Row
htmlBuilder.Append("<span style='font-size: small;font-weight:bold;'></span>\n");
htmlBuilder.AppendLine("\n<tr align='left' valign='top'>");
foreach (DataColumn targetColumn in targetTable.Columns)
{
htmlBuilder.Append("<td align='left' valign='top'>");
htmlBuilder.Append("<b>");
htmlBuilder.Append(targetColumn.ColumnName);
htmlBuilder.Append("</b>");
htmlBuilder.Append("</td>\n");
}
htmlBuilder.AppendLine("</tr>");
//Create Data Rows
foreach (DataRow myRow in targetTable.Rows)
{
htmlBuilder.AppendLine("\n<tr align='left' valign='top'>");
foreach (DataColumn targetColumn in targetTable.Columns)
{
htmlBuilder.Append("<td align='left' valign='top'>");
htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString());
htmlBuilder.Append("</td>\n");
}
//htmlBuilder.Append("</td>");
htmlBuilder.AppendLine("</tr>\n");
}
//Create Bottom Portion of HTML Document
htmlBuilder.AppendLine("</table>");
htmlBuilder.AppendLine("</body>");
htmlBuilder.AppendLine("</html>");
//Create String to be Returned
htmlString = htmlBuilder.ToString();
return htmlString;
}
}
答案 0 :(得分:1)
//create Data Row
int count = 0;
int rowsinserted = 0;
foreach (DataRow myRow in targetTable.Rows)
{
htmlBuilder.AppendLine("\n<tr align='left' valign='top'>");
foreach (DataColumn targetColumn in targetTable.Columns)
{
if (targetColumn == targetTable.Columns[0])
{
if (count == 0)
{
htmlBuilder.Append("<td rowspan='" + targetTable.Rows.Count + "' 'align='left' valign='top'>");
htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString());
htmlBuilder.Append("</td>\n");
}
}
else
{
int columncount = 1;
for (int i = 1; i < targetTable.Columns.Count; i++)
{
if (targetColumn == targetTable.Columns[columncount])
{
htmlBuilder.Append("<td 'align='left' valign='top'>");
htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString());
htmlBuilder.Append("</td>\n");
}
columncount = columncount + 1;
count = count + 1;
}
}
}
rowsinserted = rowsinserted + 1;
htmlBuilder.AppendLine("</tr>\n");
}
答案 1 :(得分:0)
我认为以下应该可行,但我没有测试过。基本上我试图确定何时第一次出现第一列中的值并添加一个跨越尽可能多的行的单元格。这仅在行按第一列排序时才有效。
//Create Data Rows
foreach (DataRow myRow in targetTable.Rows)
{
htmlBuilder.AppendLine("\n<tr align='left' valign='top'>");
foreach (DataColumn targetColumn in targetTable.Columns)
{
// if its the first column then only add a cell when its the first time
// value appears, else just add the cell
if(targetColumn == targetTable.Columns.First())
{
// if its the first time the value has appeared in the first column
// then add cell with rowspan set to the number of appearances
if(myRow == targetTable.Rows.First(r => r[targetColumn.ColumnName].ToString() == myRow[targetColumn.ColumnName].ToString()))
{
rowspan = targetTable.Rows.Count(r => r[targetColumn.ColumnName].ToString() == myRow[targetColumn.ColumnName].ToString());
htmlBuilder.Append("<td rowspan='"+rowspan+"' align='left' valign='top'>");
htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString());
htmlBuilder.Append("</td>\n");
}
}
else
{
htmlBuilder.Append("<td align='left' valign='top'>");
htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString());
htmlBuilder.Append("</td>\n");
}
}
htmlBuilder.AppendLine("</tr>\n");
}
编辑:假设您不必使用DataTable
这样可行,我已对其进行了测试,您可以查看输出here。我假设您不必使用DataTable
,因为使用List
和Dictionary
等枚举变得容易得多。理想情况下,您可能希望创建自己的类而不是使用Dictionary
,但我认为它可以用于演示目的。
var Data = new List<Dictionary<string, string>>();
Data.Add(new Dictionary<string, string>() { {"Company", "Company 1"}, {"Name", "Jim"}, {"Gender", "m"} });
Data.Add(new Dictionary<string, string>() { {"Company", "Company 1"}, {"Name", "Bob"}, {"Gender", "m"} });
Data.Add(new Dictionary<string, string>() { {"Company", "Company 1"}, {"Name", "Dawn"}, {"Gender", "f"} });
Data.Add(new Dictionary<string, string>() { {"Company", "Company 2"}, {"Name", "Dave"}, {"Gender", "m"} });
Data.Add(new Dictionary<string, string>() { {"Company", "Company 2"}, {"Name", "Ben"}, {"Gender", "m"} });
Data.Add(new Dictionary<string, string>() { {"Company", "Company 2"}, {"Name", "Tina"}, {"Gender", "f"} });
Data.Add(new Dictionary<string, string>() { {"Company", "Company 2"}, {"Name", "Sarah"}, {"Gender", "f"} });
string html = "<table><tr>";
foreach (var Column in Data.First().Select(col => col.Key))
{
html += string.Format("<th>{0}</th>", Column);
}
html += "</tr>";
foreach (var Row in Data)
{
html += "<tr>";
foreach (string Column in Row.Select(col => col.Key))
{
if(Column == Row.First().Key)
{
if(Row == Data.First(row => row.First(col => col.Key == Column).Value == Row.First(col => col.Key == Column).Value))
{
int Row_Span = Data.Count(row => row.First(col => col.Key == Column).Value == Row.First(col => col.Key == Column).Value);
html += string.Format("<td rowspan='{0}'>{1}</td>", Row_Span, Row.First(col => col.Key == Column).Value);
}
}
else
{
html += string.Format("<td>{0}</td>", Row.First(col => col.Key == Column).Value);
}
}
html += "</tr>";
}