亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 epplus 導出時動態合并行

使用 epplus 導出時動態合并行

C#
開滿天機 2022-07-10 16:12:31
我想使用 epplus 合并導出的 excel 中的行。下面的代碼僅在我在一列中只有一個相同的值時才有效。例如(我在 Col1 行中合并相同的值):但是,如果我有另一個這樣的表,則代碼在合并時出錯(我在 Col1 和 Col2 行中合并相同的值)請幫我修復代碼。 void mergeCells(DataTable dt, int startIndex, int totalColumns, ExcelWorksheet ws)    {            if (totalColumns == 0) return;            int i, count = 1;            ArrayList lst = new ArrayList();            lst.Add(ws.Cells[2, 1]);            var ctrl = ws.Cells[startIndex + 1, 1];            for (i = 1; i <= dt.Rows.Count; i++)            {                ExcelRange nextMerge = ws.Cells[i + totalColumns + 1, 1];                if (ctrl.Text == nextMerge.Text)                {                    count++;                    lst.Add(ws.Cells[i, 1]);                }                else                {                    if (count > 1)                    {                        ws.Cells[i + 1, 1, i + count, 1].Merge = true;                        mergeCells(new DataTable(lst.ToString()), startIndex + count, totalColumns, ws);                    }                    count = 1;                    lst.Clear();                    ctrl = ws.Cells[i + 2, startIndex];                    lst.Add(ws.Cells[i, 1]);                }            }            if (count > 1)            {                ws.Cells[startIndex + 1, 1, startIndex + count, 1].Merge = true;                mergeCells(new DataTable(lst.ToString()), startIndex + count, totalColumns - 1, ws);            }            count = 1;            lst.Clear();        }
查看完整描述

1 回答

?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

我已經這樣做了:


public void WriteDataToSheet(DataTable data)

{

    using (ExcelPackage excel = new ExcelPackage())

    {

        ExcelWorksheet ws = excel.Workbook.Worksheets.Add("Test");


        ws.Cells["A1"].LoadFromDataTable(data, true);

        ws.Cells[ws.Dimension.Address].AutoFitColumns();


        var listObject = data.AsEnumerable()

                .Select(x => new

                {

                    Col1 = x.Field<string>("Col1"),

                    Col2 = x.Field<string>("Col2"),

                    Col3 = x.Field<string>("Col3")

                }).ToList();



        var lisa = listObject.GroupBy(x => x.Col1).

            Select(x => new

            {

                Id = x.Key,

                Quantity = x.Count(),

                secondGroup = x.GroupBy(y => y.Col2)

                           .Select(y => new

                           {

                               ID = y.Key,

                               secondGroup = y.Count()

                           })

            });


        int A = 1, B = 0, C = 1, D = 0;

        foreach (var item in lisa)

        {

            B = A + 1;

            A += item.Quantity;

            ws.Cells["A" + B + ":A" + A + ""].Merge = true;

            ws.Cells["B" + B + ":B" + A + ""].Merge = true;


            foreach (var item2 in item.secondGroup)

            {

                D = C + 1;

                C += item2.secondGroup;

                ws.Cells["C" + D + ":C" + C + ""].Merge = true;

            }

        }

        // Save merged and modified file to the location

    }

}


查看完整回答
反對 回復 2022-07-10
  • 1 回答
  • 0 關注
  • 230 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號