第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 epplus 導(dǎo)出時(shí)動(dòng)態(tài)合并行

使用 epplus 導(dǎo)出時(shí)動(dòng)態(tài)合并行

C#
開(kāi)滿(mǎn)天機(jī) 2022-07-10 16:12:31
我想使用 epplus 合并導(dǎo)出的 excel 中的行。下面的代碼僅在我在一列中只有一個(gè)相同的值時(shí)才有效。例如(我在 Col1 行中合并相同的值):但是,如果我有另一個(gè)這樣的表,則代碼在合并時(shí)出錯(cuò)(我在 Col1 和 Col2 行中合并相同的值)請(qǐng)幫我修復(fù)代碼。 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貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

我已經(jīng)這樣做了:


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

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-07-10
  • 1 回答
  • 0 關(guān)注
  • 226 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)