处理Excel导出中的空单元格

时间:2018-04-03 09:15:09

标签: asp.net excel export isnullorempty

我的asp.net应用程序中有一个导出功能。我想删除列表中的所有空格/空值。有什么方法可以修剪它吗?

这就是我的代码的样子:

我的数据库中的GetData方法:

          public void GetData()
       {
        try
        {
            DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["GS1connectionString"].ConnectionString);
            connection.Open();
            using (SqlCommand sqlCmd = new SqlCommand("DAtabase.dbo.SP_EXPORT", connection)) //Extract data: no cons first and cons after
            {
                sqlCmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd))
                {
                    sqlDa.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        Session["TaskTable"] = dt;
                    }
                }
            }
        }
        catch (Exception)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('No data found, try again');", true);
        }
    }

清理dataTable方法:

    public DataTable removeEmptyColumns(DataTable table)
    {
        int k = 0, l = 0;
        DataTable dtResult = table.Clone();
        DataRow row = dtResult.NewRow();
        row = dtResult.NewRow();
        for (k = 0; k < table.Rows.Count; k++)
        {
            for (l = 0; l < table.Rows.Count; l++)
            {
                if (k == l)
                    row[k] = table.Rows[k][l];
            }
        }
        dtResult.Rows.Add(row);
        dtResult.AcceptChanges();
        return dtResult;
    }

导出到Excel按钮:

    protected void Button_Export_DS_Click(object sender, EventArgs e)
    {
        GetData();
        DataTable dat = new DataTable();
        DataTable dtNew = new DataTable();
        dat = (DataTable)Session["TaskTable"];
        dtNew = removeEmptyColumns(dat);
        //Export to excel from datatable stored in a session


            if (dtNew.Rows.Count > 0)
            {
                MemoryStream ms = new MemoryStream();
                int i = 1;
                using (ExcelPackage package = new ExcelPackage(ms))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet" + i++);
                    worksheet.Cells["A1"].LoadFromDataTable(dtNew, true);
                    worksheet.Cells.AutoFitColumns();

                    Response.Clear();
                    package.SaveAs(Response.OutputStream);
                    Response.AddHeader("content-disposition", "attachchment; filename=DS_Export.xls");
                    Response.Charset = "";
                    Response.ContentType = "application/vnd.xls";
                    Response.End();
                }
            }

    }

我的asp.net应用程序中有一个导出功能。我想删除列表中的所有空格/空值。有什么方法可以修剪它吗?

1 个答案:

答案 0 :(得分:0)

您复制并粘贴此功能:

public DataTable removeEmptyColumns(DataTable table)
{
    int k = 0, l = 0;
    DataTable dtResult = table.Clone();
    DataRow row = dtResult.NewRow();
    row = dtResult.NewRow();
    for (k = 0; k < table.Rows.Count ; k++)
    {
        for (l = 0; l < table.Rows.Count; l++)
        {
            if (k == l)
                row[k] = table.Rows[k][l];
        }
    }
    dtResult.Rows.Add(row);
    dtResult.AcceptChanges();
    return dtResult;
}

然后使用这样的函数:

DataTable dt = new DataTable();
DataTable dtNew = new DataTable();
dt = (DataTable)Session["TaskTable"];
dtNew = removeEmptyColumns(dt);

之后,在此使用此dtNew:

if (dtNew.Rows.Count > 0)
{
    MemoryStream ms = new MemoryStream();
    int i = 1;
    using (ExcelPackage package = new ExcelPackage(ms))
    {
        ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet" + i++);
        worksheet.Cells["A1"].LoadFromDataTable(dtNew, true);
        worksheet.Cells.AutoFitColumns();

        Response.Clear();
        package.SaveAs(Response.OutputStream);
        Response.AddHeader("content-disposition", "attachchment; filename=Export.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        Response.End();
    }
}