急急急!!!求高手解决!!!从一个DataGrid中把数据导入了一个Excel文件,在Excel显示时怎样把相同数据的单元格合并

gxjgxjgxj 2007-07-05 01:16:14
在ASP.NET中,我已把页面中一个DataGrid中的数据导入到一个Excel文件,现在想怎样在Excel显示时把一些同一类数据的单元格合并,
例如:有一Students表当中有一字段Class,现有几个学生班级都是1,我就想在Excel中把1的单元格合并,有那位高手帮我解决一下吗?谢谢了!
...全文
297 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jinglecat 2007-08-21
  • 打赏
  • 举报
回复
先在 DataGrid 中合并再导出

// 合并实例

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>

<%-- http://community.csdn.net/Expert/TopicView3.asp?id=5720389 --%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
LoadProductData();
}
}

void LoadProductData()
{
DataTable dt = CreateSampleProductData();

GridView1.DataSource = dt;
GridView1.DataBind();

DataGrid1.DataSource = dt;
DataGrid1.DataBind();

}

#region sample data

static DataTable CreateSampleProductData()
{
DataTable tbl = new DataTable("Products");

tbl.Columns.Add("ProductID", typeof(int));
tbl.Columns.Add("ProductName", typeof(string));
tbl.Columns.Add("UnitPrice", typeof(decimal));
tbl.Columns.Add("CategoryID", typeof(int));

tbl.Rows.Add(1, "Chai", 18, 1);
tbl.Rows.Add(2, "Chang", 19, 1);
tbl.Rows.Add(3, "Aniseed Syrup", 10, 2);
tbl.Rows.Add(4, "Chef Anton's Cajun Seasoning", 22, 2);
tbl.Rows.Add(5, "Chef Anton's Gumbo Mix", 21.35, 2);
tbl.Rows.Add(47, "Zaanse koeken", 9.5, 3);
tbl.Rows.Add(48, "Chocolade", 12.75, 3);
tbl.Rows.Add(49, "Maxilaku", 20, 3);

return tbl;
}

#endregion

int toMergedRows = 1;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex > 0) {
DataRowView currentRowView = e.Row.DataItem as DataRowView;
DataView dv = currentRowView.DataView;
if((int)currentRowView["CategoryId"] == (int)dv[e.Row.DataItemIndex - 1]["CategoryId"]) {
toMergedRows++;
}
else {
GridView1.Rows[e.Row.RowIndex - toMergedRows].Cells[0].RowSpan = toMergedRows;
for(int ndx = e.Row.RowIndex - toMergedRows + 1; ndx < e.Row.RowIndex; ndx++) {
GridView1.Rows[ndx].Cells.RemoveAt(0);
}
toMergedRows = 1;
}

// 已到最后一行
if(e.Row.DataItemIndex == dv.Count - 1 && toMergedRows !=1) {
GridView1.Rows[e.Row.RowIndex - toMergedRows + 1].Cells[0].RowSpan = toMergedRows;
for(int ndx = e.Row.RowIndex - toMergedRows + 2; ndx < e.Row.RowIndex; ndx++) {
GridView1.Rows[ndx].Cells.RemoveAt(0);
}
e.Row.Cells.RemoveAt(0);
}
}
}

int toMergedRows2 = 1;
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
DataRowView currentRowView = e.Item.DataItem as DataRowView;
if(currentRowView != null && e.Item.ItemIndex > 0) {
DataView dv = currentRowView.DataView;
if((int)currentRowView["CategoryId"] == (int)dv[e.Item.DataSetIndex - 1]["CategoryId"]) {
toMergedRows2++;
}
else {
DataGrid1.Items[e.Item.ItemIndex - toMergedRows2].Cells[0].RowSpan = toMergedRows2;
for(int ndx = e.Item.ItemIndex - toMergedRows2 + 1; ndx < e.Item.ItemIndex; ndx++) {
DataGrid1.Items[ndx].Cells.RemoveAt(0);
}
toMergedRows2 = 1;
}

// 已到最后一行
if(e.Item.DataSetIndex == dv.Count - 1 && toMergedRows2 != 1) {
DataGrid1.Items[e.Item.ItemIndex - toMergedRows2 + 1].Cells[0].RowSpan = toMergedRows2;
for(int ndx = e.Item.ItemIndex - toMergedRows2 + 2; ndx < e.Item.ItemIndex; ndx++) {
DataGrid1.Items[ndx].Cells.RemoveAt(0);
}
e.Item.Cells.RemoveAt(0);
}
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>CSDN_MergeGridViewRow2</title>

</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
</Columns>
</asp:GridView>

<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="false" OnItemDataBound="DataGrid1_ItemDataBound">
<Columns>
<asp:BoundColumn DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundColumn DataField="ProductName" HeaderText="ProductName" />
<asp:BoundColumn DataField="UnitPrice" HeaderText="UnitPrice" />
</Columns>
</asp:DataGrid>

</div>

</form>
</body>
</html>
JGood 2007-08-20
  • 打赏
  • 举报
回复
Excel中合并,用ASP.NET

======
阿拉不会哦
nbshiny 2007-08-20
  • 打赏
  • 举报
回复
想知道答案,没有达人知道吗?
NekChan 2007-07-05
  • 打赏
  • 举报
回复
如果是oracle,可以采用 count(1) over(partition by 班级编号 order by 班级编号)得到具体的数量,如下:
班级编号 姓名 数量
1 aaa 3
bbb 3
ccc 3
2 ddd 2
eee 2
3 fff 1

然后就在生成 HtmlTable 的时候,根据“班级编号”设定他的rowspan=数量
Jinglecat 2007-07-05
  • 打赏
  • 举报
回复
应该先在 DG 中合并,再导出
gxjgxjgxj 2007-07-05
  • 打赏
  • 举报
回复
差不多,不要打击我啊,有高手帮帮忙吗
F15Eagle 2007-07-05
  • 打赏
  • 举报
回复
就像报表中的相同项向下合并吗?这个就难了
gxjgxjgxj 2007-07-05
  • 打赏
  • 举报
回复
班级编号 姓名
1 aaa
1 bbb
1 ccc
2 ddd
2 eee
3 fff

班级编号 姓名
1 aaa
bbb
ccc
2 ddd
eee
3 fff

就是这种效果

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧