请帮帮我。
我想知道如何将我的数据表导出到没有标题或排序行的excel工作表(这是第1行)。
这是我的代码:
protected void Button2_Click(object sender, EventArgs e) {
string constr = ConfigurationManager.ConnectionStrings["bscConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(SqlDataSource1.SelectCommand);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter();
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
cmd.CommandText = "FechasRango1";
cmd.Parameters.AddWithValue("@Fecha", TextBox1.Text);
cmd.Parameters.AddWithValue("@Fecha2", TextBox2.Text);
sda.Fill(dt);
if (dt.Rows.Count == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('No Hay Resultados');</script>");
}
else if (dt.Rows.Count != 0)
{
XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add(dt, "Customers");
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=SqlExport.xlsx");
MemoryStream MyMemoryStream = new MemoryStream();
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
答案 0 :(得分:0)
我很确定您提供的这一行代码为您提供了刚才添加的工作表:
wb.Worksheets.Add(dt,“Customers”);
将其保存在变量中,让工作表删除第一行:
sheet.Range("A1", "A1").EntireRow.Delete();
看起来您正在使用ClosedXML库。 Here您会找到很多关于如何删除行的示例。