gpt4 book ai didi

c# - 在excel中扩展列的代码

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:16 25 4
gpt4 key购买 nike

当我将数据导出到 excel 时,它会加载列但不会展开列。它无需双击行即可显示数据以展开列。我可以编写一些代码来自动调整列,以便单元格可以显示整个数据吗?我正在使用 asp.net 和 c#。

第一张图片是目前的样子,第二张是我想要的样子。

What it looks like

What I want it to look like

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;

DataSet dataSet = new DataSet();

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ISALog1ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("exec ProxyReport", conn);
cmd.CommandTimeout = 200;
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dataSet);

GridView1.DataSource = dataSet.Tables[0];
GridView1.DataBind();
GridView2.DataSource = dataSet.Tables[1];
GridView2.DataBind();
GridView3.DataSource = dataSet.Tables[2];
GridView3.DataBind();

dataSet.Tables[0].TableName = "1";
dataSet.Tables[1].TableName = "2";
dataSet.Tables[2].TableName = "3";

int count = 3;

ExcelPackage pack = new ExcelPackage();


for (int i = 1; i <= count; i++)
{
DataTable table = dataSet.Tables[i.ToString()];
ExcelWorksheet ws = pack.Workbook.Worksheets.Add("Top" + i);
ws.Cells["A1"].LoadFromDataTable(table, true);
}

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExcelExport.xlsx");
Response.BinaryWrite(pack.GetAsByteArray());
Response.Flush();
Response.End();
}

最佳答案

Excel.Range range;

range = ws.get_Range("A1", "1");
range.AutoFit();

MSDN

关于c# - 在excel中扩展列的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11474100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com