iT邦幫忙

0

【已解答】請問如何讓class類別的欄位動態建立?

  • 分享至 

  • xImage

我目前在練習MVC的Excel操作物件的匯入匯出
用的是Npoi的參考
我想問問這class類別的欄位能否動態建立?

否則我可能用 new XSSFWorkbook() 這方法試試
第一列的欄位名稱建立
第二列的資列輸入

var currentRow = 0; // 開始行號-建立欄位名
sheet.Cell(currentRow, 0).SetCellValue("編號");
sheet.Cell(currentRow, 1).SetCellValue("姓名");
sheet.Cell(currentRow, 2).SetCellValue("性別");
→動態欄位
var currentRow = 1; // 建立資料
sheet.Cell(currentRow, 0).SetCellValue(dr["編號"].ToString());
sheet.Cell(currentRow, 1).SetCellValue(dr["姓名"].ToString());
sheet.Cell(currentRow, 2).SetCellValue(dr["性別"].ToString());
→根據動態欄位匯出的資料

目前Googlet查到Npoi的相關作法~

https://ithelp.ithome.com.tw/upload/images/20240508/20061369i5KV3Vzuci.png

匯出後得到此Excel

https://ithelp.ithome.com.tw/upload/images/20240508/200613693K5QeTGdTe.png

我改成從資料庫SQL匯出後

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult ReportSet(AttendanceStatisticsMonthReportModel model)
        {
            var students = new List<Student>();
            var Sql = @"
                select Id,Name,Sex
                from Students
            ";
            var dt = MSSQLHelper.GetTable(Sql, System.Data.CommandType.Text);
            if (dt.Rows.Count > 0)
            {
                foreach (System.Data.DataRow dr in dt.Rows)
                {
                    students.Insert(0
                    , new Student
                    {
                        Id = dr["Id"].ToString(),
                        Name = dr["Name"].ToString(),
                        Sex = dr["Sex"].ToString()
                    });
                }
            }
            var mapper = new Mapper(); //建立Excel操作物件
            var stream = new MemoryStream(); //建立IO檔案資料流
            //将students陣列生成的Excel資料,直接放置到Stream資料流
            mapper.Save(stream, students, "sheet1", overwrite: true, xlsx: true);
            //下載Excel檔案
            return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Student.xlsx");
        }

        public class Student
        {
            [Column("學號")]
            public string Id { get; set; }
            [Column("姓名")]
            public string Name { get; set; }
            [Column("性別")]
            public string Sex { get; set; }
        }

驗證此匯出有效~
https://ithelp.ithome.com.tw/upload/images/20240508/20061369NTJsLAfrjK.png

那麼可能將 class類別 Student 改成動態欄位定義嗎?
如下圖:
https://ithelp.ithome.com.tw/upload/images/20240508/20061369iMv2YlePe4.png

已解答網址:
https://ithelp.ithome.com.tw/questions/10215682

看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2024-05-08 15:36:47 檢舉
關鍵字: jsonfield。C#應該是也能支援,寫raw sql的話更沒問題。
不過從你的範例看起來需要的是一欄備註。
我的需求是Excel欄位不是固定的~
有時候只有三個欄位~
有時候五個欄位或八個欄位~

就像網頁的表格列表~
欄位顯示會隨機變動@@(使用者可以看到的權限)
在這個 public class Student 裡面的欄位定義~不能寫死^^a
froce iT邦大師 1 級 ‧ 2024-05-08 15:59:25 檢舉
那我回答的沒錯啊,jsonfield。
MS SQL應該是有支援了。

反正我知道postgresql有。在jsonfield欄位存的就是json,所以欄位數可以不固定,也可以做篩選之類的動作。
你意思是說不要用C#功能~用MSSQL 方式匯出Excel吧~
哈~其實我前面用 new XSSFWorkbook()
使用索引 指定欄位 寫入 資料就可以了~

只是我想知道有無高手解這個 class 定義,可以動態增加屬性之類@@~
然後再將動態增加的屬性~寫入Excel裡面~
froce iT邦大師 1 級 ‧ 2024-05-08 16:52:19 檢舉
不是,是欄位的類型可以設定成存json。
https://learn.microsoft.com/zh-tw/sql/relational-databases/json/json-data-sql-server?view=sql-server-ver16
應該可以先在SQL中弄成表,再從表自動生成xlsx ,而不用先定義欄位。

你要求的應該是不容易做到。
Jsonfield 就是關聯性資料庫前一陣子動態欄位這塊被NoSQL打,後來出的對應手段
念戀 iT邦新手 5 級 ‧ 2024-05-13 20:29:22 檢舉
不能用DataTable嗎?
方法不同~
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答