.net npoi 導出合並單元格


          我寫這個是因為 一個妹子(因為我喜歡這妹子,不然誰鳥你,所以身邊對你好的異性一定對你圖謀不軌,就像我)請教我這個問題

          導出exexl,大多數人百度一下就知道怎么寫,但合並單元格就要有一定思考,我也看過大量的資料,但我覺得有解決問題的思路很重要,廢話不說了  ,直接來看代碼:  

    CellRangeAddress(y, y1, x, x1)分別代表這  合並的初始行,結束行,初始列,結束列

 

                CellRangeAddress region = new CellRangeAddress(y, y1, x, x1);//合並單元格
                sheet.AddMergedRegion(region);
                cell = sheet.GetRow(item.Ya).GetCell(2);
                cell.CellStyle = cellstyles;//合並單元格后的樣式

這里的

cellstyles合並單元格的樣式,我就不做解釋,直接貼代碼:
            //這里就是合並單元格后單元格的樣式
            //在合並單元格后  cell.CellStyle = cellstyles;   這樣就ok了  字體大小也可以在這里該
            NPOI.SS.UserModel.ICellStyle cellstyles = workbook.CreateCellStyle();//設置垂直居中格式
            cellstyles.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            cellstyles.Alignment = HorizontalAlignment.Left;//水平居左
            cellstyles = workbook.CreateCellStyle();//設置垂直居中格式
            cellstyles.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            cellstyles.Alignment = HorizontalAlignment.Left;//水平居中

上面都是湊字數,接下來說一說  我對合並上下單元格的理解想法就是 :用數組或者集合存放合並單元格的坐標,對就是這么簡單,算了直接貼代碼:

            //寫入列
            for (int i = 0; i < dts.Columns.Count; i++)
            {
                row.CreateCell(i).SetCellValue(dts.Columns[i].ColumnName);
            }
            //根據上次代碼  做的改進,用一個集合來存放 合並單元格的位置,因為  我們這只需要上下單元格合並 所以  一條數據只存放2個值,代表這 從第幾行 到第幾行  注意  后者的值必定大於等於前者
            //如  是上下左右的單元格 合並一條數據就要存放4個值  
            List<indexs> ids = new List<indexs>();
            bool flag = true; ;
            string temp = "";
            //寫入行數據
            for (int i = 0; i < dts.Rows.Count; i++)
            {
                /*
                 標題占一行
                 列名占一行
                  所以 row = sheet.CreateRow(i + 2);    這里是 i+2
                */
                row = sheet.CreateRow(i + 2);
                for (int j = 0; j < dts.Columns.Count; j++)
                {
                    if (dts.Columns[j].ColumnName == "IDNumber")//判斷是否到需要合並的單元格
                    {
                        if (i != 0)
                        {
                            if (temp != dts.Rows[i][j].ToString().Trim())
                            {
                                temp = dts.Rows[i][j].ToString().Trim();
                                if (temp == dts.Rows[i - 1][j].ToString().Trim())
                                {
                                    ids.Add(new indexs
                                    {
                                        Ya = i + 1,
                                    });
                                }
                                else
                                {
                                    if (ids.Count() != 0 && ids[ids.Count - 1].Yb == 0)
                                    {
                                        ids[ids.Count - 1].Yb = i + 1;
                                        flag = true;
                                    }
                                }
                            }
                            else
                            {
                                if (temp == dts.Rows[i - 1][j].ToString().Trim())
                                {
                                    //flag  防止出現重復
                                    if (flag)
                                    {
                                        //出現相同訂單號的位置
                                        flag = false;
                                        ids.Add(new indexs
                                        {
                                            Ya = i + 1,
                                        });
                                    }
                                    temp = dts.Rows[i][j].ToString().Trim();//感覺這里可以不寫這個  你可以試試
                                }
                                //防止最后一條數據也是合並項
                                if (i == dts.Rows.Count - 1)
                                {
                                    ids[ids.Count - 1].Yb = i + 2;
                                }
                            }
                        }
                    }
                    //寫入單元格
                    var col = row.CreateCell(j);
                    col.SetCellValue(dts.Rows[i][j].ToString().Trim());
                }
            }

            //這里就是合並單元格后單元格的樣式
            //在合並單元格后  cell.CellStyle = cellstyles;   這樣就ok了  字體大小也可以在這里該
            NPOI.SS.UserModel.ICellStyle cellstyles = workbook.CreateCellStyle();//設置垂直居中格式
            cellstyles.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            cellstyles.Alignment = HorizontalAlignment.Left;//水平居左
            cellstyles = workbook.CreateCellStyle();//設置垂直居中格式
            cellstyles.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            cellstyles.Alignment = HorizontalAlignment.Left;//水平居中

            foreach (var item in ids)
            {
                //CellRangeAddress(item.Ya, item.Yb, 0, 0) 這里的兩個0是沒行中第一個單元格   而item.Ya, item.Yb  代表第幾行到第幾行    我們需求是上下相同的訂單號合並所以只需要改變這個的前兩個值,后面兩個值  隨之遞增   
                //因為 我們這只需要合並上下單元格的序號    而序號在每行的第一個單元格  所以這里的后兩個值都是0,
                CellRangeAddress region = new CellRangeAddress(item.Ya, item.Yb, 0, 0);//合並單元格
                sheet.AddMergedRegion(region);
                cell = sheet.GetRow(item.Ya).GetCell(0);
                cell.CellStyle = cellstyles;
            }

后來補充  實體  這個可以用其他的來代替,dome里面有  但還是補充一下

        public class show
        {
            public int Id { get; set; }
            public string number { get; set; }
            public string IDNumber { get; set; }
        }

        public class indexs
        {
            public int Ya { get; set; }
            public int Yb { get; set; }
        }

 

哎!要是問我的是個男的,上面代碼一個中文字都不會存在。

還有dome寫的是合並上下單元格,但左右單元格的合並大致是一樣的

心情好給你們看看我寫的dome:

鏈接:https://pan.baidu.com/s/1zp6rVsQEn_PY2mGLalnlfA
提取碼:1580

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM