Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数据库数据格式化为同一行中的excel c_C#_Mysql_Asp.net_Excel - Fatal编程技术网

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

C# 如何将数据库数据格式化为同一行中的excel c,c#,mysql,asp.net,excel,C#,Mysql,Asp.net,Excel,这是数据库中我的表 使用此表,它将使用以下格式导出到excel,其中具有相同大小写id的记录将导出到同一行中的excel 我正在为如何使数据库中的数据显示在同一个案例id的excel的同一行而苦苦挣扎 下面是我目前正在使用的excel的一个片段 这是我的C代码 public string DB_CONN = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; protected void Page_L

这是数据库中我的表

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

我正在为如何使数据库中的数据显示在同一个案例id的excel的同一行而苦苦挣扎

下面是我目前正在使用的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();
        }

    }

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

非常感谢你的帮助!!
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;
}