for (i = 0; i < table.Rows.Count; i++)
{
bidName = table.Rows[index]["BIDNAME"].ToString();
if (table.Rows[i]["BIDNAME"].ToString() == bidName)
{
projNum++;
worksheet.Cells[5 + i, 2] = table.Rows[i]["PROJNO"];
worksheet.Cells[5 + i, 3] = table.Rows[i]["PROJNAME"];
worksheet.Cells[5 + i, 4] = table.Rows[i]["STAT_DATE"];
worksheet.Cells[5 + i, 5] = table.Rows[i]["PROJTYPE"];
worksheet.Cells[5 + i, 6] = table.Rows[i]["CONTENT"];
worksheet.Cells[5 + i, 7] = table.Rows[i]["OPENDT"];
worksheet.Cells[5 + i, 8] = table.Rows[i]["OPENADDRESS"];
worksheet.Cells[5 + i, 9] = table.Rows[i]["REV_DATE"];
worksheet.Cells[5 + i, 10] = table.Rows[i]["BID_UNIT"];
worksheet.Cells[5 + i, 11] = table.Rows[i]["AGT_AMOUNT"];
worksheet.Cells[5 + i, 12] = table.Rows[i]["CURRENCY"] + ":" + table.Rows[i]["BIDSER_AMOUNT"];
worksheet.Cells[5 + i, 13] = table.Rows[i]["SENDDATE"];
worksheet.Cells[5 + i, 14] = table.Rows[i]["CURRENCY"] + ":" + table.Rows[i]["BIDPRICE"];
worksheet.Cells[5 + i, 15] = table.Rows[i]["BOOKAMOUNT"];
worksheet.Cells[5 + i, 16] = table.Rows[i]["CURRENCY"] + ":" + table.Rows[i]["BAIL_AMOUNT"];
worksheet.Cells[5 + i, 17] = table.Rows[i]["USERNAME"];
worksheet.Cells[5 + i, 18] = table.Rows[i]["SECOND_USER"];
worksheet.Cells[5 + i, 19] = "";
worksheet.get_Range(worksheet.Cells[5 + i, 1], worksheet.Cells[5 + i, 19]).Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
continue;
}
worksheet.get_Range(worksheet.Cells[5 + rowid, 1], worksheet.Cells[5 + i - 1, 1]).Merge(Missing.Value); //将第一列按投标单位合并
worksheet.get_Range(worksheet.Cells[5 + rowid, 1], worksheet.Cells[5 + rowid, 1]).Value2 = bidName + "(" + projNum.ToString() + "个项目)";//合并后的单元格内容
合并单元格的时候也要注意一个问题,就是合并的单元格必须是为空的,不然在执行合并时,会提示“合并后的单元格的值将丢失”,具体不这样提示的,大致是这个意思,一般我们合并都单元格相同的内容,在合并前我们先保存那个值,再清空后合并,上面的代码中把worksheet.Cell[5+rowid,1]这里系列的单元格的值空出来了,没写数据,而且最后合并了再写值,避免了去循环清空。
2.嵌套的合并向上面那样做可能控制比较麻烦,而且思路可能很混乱,我们可以考虑先循环填充所有的数据,在循环出来要合并的列,比如像下面的这张表
先循环填充数据,如下:
复制代码 代码如下: