gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:07:04 27 4
gpt4 key购买 nike

这是我在数据库中的表

enter image description here

使用此表,它将使用以下格式导出到 Excel,其中具有相同 case_id 的记录将导出到 Excel 中的同一行

Desired Output

enter image description here

我正在努力解决如何使数据库中的数据显示在 Excel 的同一行相同 case_id

这是我当前正在处理的 Excel 片段

enter image description here

这是我的 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();
}

}

最佳答案

检查上一行是否具有相同的案例 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;
}

关于c# - 如何将数据库数据格式化为excel同一行c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51749529/

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