如何将数据库数据格式化为同一行的excel c#

col17t5w  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(359)

这是我在数据库中的表

使用此表,它将使用以下格式导出到excel,其中具有相同大小写id的记录将导出到excel中的同一行

Desired Output


我正在为如何使数据库中的数据显示在excel的同一行中的同一个case\u id而苦苦挣扎
下面是我目前正在做的excel的一个片段

这是我的c代码

public string DB_CONN =
  ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        getAll();
    }

    protected void getAll()
    {

        DateTime dTime = DateTime.Now;
        string now_time = dTime.ToString("yyyy-MM-dd HH:mm:ss");
        string sql = "";
        MySqlConnection conn = null;
        MySqlDataAdapter msda = null;
        DataSet ds = null;

        string id = "ALL";

        sql = "SELECT * FROM testing";

        conn = new MySqlConnection(DB_CONN);
        conn.Open();

        msda = new MySqlDataAdapter(sql, conn);
        ds = new DataSet();
        msda.Fill(ds, "charge");

        using (ExcelPackage p = new ExcelPackage())
        {
            ExcelWorksheet worksheet = p.Workbook.Worksheets.Add("收入報表");

            int row = 1;

            //Add the headers

            worksheet.Cells[row, 1].Value = "date";
            worksheet.Cells[row, 2].Value = "heading";
            worksheet.Cells[row, 3].Value = "detail";
            worksheet.Cells[row, 4].Value = "heading";
            worksheet.Cells[row, 5].Value = "detail";
            worksheet.Cells[row, 6].Value = "heading";
            worksheet.Cells[row, 7].Value = "detail";

            for (int i = 0; i < ds.Tables["charge"].Rows.Count; i++)
            {

                ++row;
                int k = 0;
                worksheet.Cells[row, ++k].Value = ds.Tables["charge"].Rows[i]["date"].ToString();

                worksheet.Cells[row, ++k].Value = ds.Tables["charge"].Rows[i]["heading"].ToString();
                worksheet.Cells[row, ++k].Value = ds.Tables["charge"].Rows[i]["detail"].ToString();
                worksheet.Cells[row, ++k].Value = ds.Tables["charge"].Rows[i]["case_id"].ToString();

            }

            String exportFileName = "income_report_" + "_" + ".xlsx";

            Byte[] fileBytes = p.GetAsByteArray(); //Read the Excel file in a byte array

            //Clear the response
            Response.ClearHeaders();
            Response.ClearContent();
            Response.Clear();

            Response.AddHeader("Content-Disposition", "attachment;filename=" + exportFileName + "; "

            );
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            Response.BinaryWrite(fileBytes);
            Response.End();
            Response.Flush();
            Response.Close();
        }

    }
sg3maiej

sg3maiej1#

检查前一行是否与case id相同,并碰撞列而不是碰撞行。

var lastColumn;
var row = 0;
for (var i = 0; i < ds.Tables["charge"].Rows.Count; i++)
{
    if (i != 0 && ds.Tables["charge"].Rows[i]["case_id"].ToString() == ds.Tables["charge"].Rows[i - 1]["case_id"].ToString())
    {
        worksheet.Cells[row, lastColumn++] = ds.Tables["charge"].Rows[i]["heading"].ToString();
        worksheet.Cells[row, lastColumn++] = ds.Tables["charge"].Rows[i]["detail"].ToString();
        continue;
    }
    row++;
    worksheet.Cells[row, 1] = ds.Tables["charge"].Rows[i]["date"].ToString();
    worksheet.Cells[row, 2] = ds.Tables["charge"].Rows[i]["heading"].ToString();
    worksheet.Cells[row, 3] = ds.Tables["charge"].Rows[i]["detail"].ToString();
    lastColumn = 3;
}

相关问题