当我将数据导出到 excel 中时,它会加载列但不会扩展列。它显示数据而无需双击该行来展开列。我可以编写一些代码来自动调整列,以便单元格可以显示整个数据吗?我正在使用 asp.net 和 c#。
第一张照片是它目前的样子,第二张显示了我想要的样子。
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();
}