網(wǎng)站首頁 編程語言 正文
場(chǎng)景
在實(shí)際開發(fā)過程中,特別是接口對(duì)接之類的,對(duì)于這種需求是屢見不鮮,現(xiàn)在很多在線平臺(tái)也都提供了像json轉(zhuǎn)實(shí)體、sql轉(zhuǎn)實(shí)體等。但是很多情況下,我們接收到的其實(shí)都是一份接口文檔,在文檔中利用表格標(biāo)明了字段的名稱、備注、類型等,而關(guān)于json什么的都是后來才有的,或者說,傳輸根本不用json。因此,表格數(shù)據(jù)能夠轉(zhuǎn)成實(shí)體類的需求就比較明顯了。
需求
所以,綜上場(chǎng)景所述,我們需要一個(gè)小工具,可以將表格數(shù)據(jù)直接轉(zhuǎn)換為c#代碼,當(dāng)然,本著通用化的思想,小工具當(dāng)然不會(huì)單純的做一個(gè)讀取excel文件的功能,這樣一點(diǎn)也不好用,因?yàn)橛善渌胤教峁┑奈臋n有的是excel,有的是word,所以,我們利用剪切板來做,只要解析剪切板的數(shù)據(jù)就可以了。
開發(fā)環(huán)境
.NET Framework版本:4.5
開發(fā)工具
?Visual Studio 2013
實(shí)現(xiàn)代碼
public class GeneratorFieldModel { public string FieldDesc { get; set; } public string Modifier { get { return "public"; } } public string Type { get; set; } public string FieldName { get; set; } public string Property { get { return "{ get; set; }"; } } public bool IsNull { get; set; } public bool IsKey { get; set; } public string DefaultText { get; set; } readonly string startDesc = "\t\t/// <summary>"; readonly string endDesc = "\t\t/// </summary>"; public string FieldDescText { get { List<string> list = new List<string>(); list.Add(startDesc); list.Add("\t\t///" + FieldDesc + ""); list.Add(endDesc); return "\r\n" + string.Join("\r\n", list); } } public string PropertyText { get { return "\t\t" + string.Join(" ", Modifier, Type + (IsNull ? "?" : ""), FieldName, Property); } } }
public partial class Form_ToEntity : Form { BindingList<Entity> bindData = new BindingList<Entity>(); public Form_ToEntity() { InitializeComponent(); } private void Form_ToEntity_Load(object sender, EventArgs e) { string[] types = new string[]{ "string", "decimal", "double", "int", "bool", "long" }; DataGridViewComboBoxColumn dgvComboBox = Column2 as DataGridViewComboBoxColumn; dgvComboBox.Items.AddRange(types); dataGridView1.DataSource = bindData; } #region 處理點(diǎn)擊選中著色 private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { DataGridViewColumn selectColumn = dataGridView1.Columns[e.ColumnIndex]; Color color = selectColumn.DefaultCellStyle.BackColor == Color.LightGray ? Color.White : Color.LightGray; selectColumn.DefaultCellStyle.BackColor = color; selectColumn.HeaderCell.Style.BackColor = color; selectColumn.Tag = color; } private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex == -1 && e.ColumnIndex > -1) { DataGridViewColumn selectColumn = dataGridView1.Columns[e.ColumnIndex]; Color color = selectColumn.Tag == null ? Color.White : (Color)selectColumn.Tag; e.CellStyle.BackColor = color; } } #endregion /// <summary> /// 獲取剪切板數(shù)據(jù) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.V) { try { string text = Clipboard.GetText(); if (string.IsNullOrWhiteSpace(text)) { return; } string[] clipData = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); bindData = Clip2Entity(clipData); dataGridView1.DataSource = new BindingList<Entity>(bindData); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } /// <summary> /// 將剪切板數(shù)據(jù)轉(zhuǎn)換為表格數(shù)據(jù) /// </summary> /// <param name="data"></param> /// <returns></returns> private BindingList<Entity> Clip2Entity(string[] data) { BindingList<Entity> list = new BindingList<Entity>(); foreach (string s in data) { Entity entity = new Entity(); string[] arr = s.Split('\t'); if (arr.Length == 2) { //選中名稱和類型 if (isCheck(dataGridView1.Columns[0]) && isCheck(dataGridView1.Columns[1])) { entity.name = arr[0]; entity.type = arr[1].ToLower(); entity.remark = ""; } //選中名稱和備注 if (isCheck(dataGridView1.Columns[0]) && isCheck(dataGridView1.Columns[2])) { entity.name = arr[0]; entity.type = "string"; entity.remark = arr[1]; } //選中類型和備注 if (isCheck(dataGridView1.Columns[1]) && isCheck(dataGridView1.Columns[2])) { entity.name = ""; entity.type = arr[0].ToLower(); entity.remark = arr[1]; } } else if (arr.Length == 3) { entity.name = arr[0]; entity.type = arr[1].ToLower(); entity.remark = arr[2]; } else { if (isCheck(dataGridView1.Columns[0])) { entity.name = s; entity.type = "string"; entity.remark = ""; } else if (isCheck(dataGridView1.Columns[1])) { entity.name = ""; entity.type = s.ToLower(); entity.remark = ""; } else if (isCheck(dataGridView1.Columns[2])) { entity.name = ""; entity.type = "string"; entity.remark = s; } } list.Add(entity); } return list; } /// <summary> /// 判斷列是否被選中 /// </summary> /// <param name="column"></param> /// <returns></returns> private bool isCheck(DataGridViewColumn column) { if (column.DefaultCellStyle.BackColor == Color.LightGray) { return true; } else { return false; } } private class Entity { public string name { get; set; } public string type { get; set; } public string remark { get; set; } } private void btn_add_Click(object sender, EventArgs e) { bindData.Add(new Entity { type = "string" }); } private void btn_delete_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in dataGridView1.SelectedRows) { dataGridView1.Rows.Remove(row); } } private void btn_generate_Click(object sender, EventArgs e) { StringBuilder stringBuilder = new StringBuilder(); foreach (Entity entity in bindData) { GeneratorFieldModel field = new GeneratorFieldModel { FieldName = entity.name, FieldDesc = entity.remark, Type = entity.type }; stringBuilder.AppendLine(field.FieldDescText); stringBuilder.AppendLine(field.PropertyText); } string path = Application.StartupPath + "\\entity.txt"; File.WriteAllText(path, stringBuilder.ToString()); Process.Start(path); } }
實(shí)現(xiàn)效果
代碼解析:首先我們定義了一個(gè)GeneratorFieldModel
類,在這個(gè)類中根據(jù)不同的字段屬性進(jìn)行了代碼的拼接,這樣就可以很方便的調(diào)用,直接把值傳進(jìn)去即可得到要生成的實(shí)體代碼,然后在Ui中,首先處理了一下選中變色(標(biāo)識(shí)我們要處理哪些數(shù)據(jù)列),然后就是分析剪切板數(shù)據(jù),轉(zhuǎn)化成需要的結(jié)構(gòu)化數(shù)據(jù)(表格復(fù)制到剪切板的數(shù)據(jù),其實(shí)就是以"\r\n"
來分割的),顯示到dataGridView中。
原文鏈接:https://mp.weixin.qq.com/s/5XG3p3UtozGUneCcN3DjHA
相關(guān)推薦
- 2022-07-12 修改docker官方鏡像內(nèi)部內(nèi)容并重新build鏡像
- 2022-04-30 Python中類變量和實(shí)例變量的區(qū)別_python
- 2023-03-26 Redis?分片集群的實(shí)現(xiàn)_Redis
- 2023-05-22 pytorch的Backward過程用時(shí)太長問題及解決_python
- 2022-10-26 Jenkins+Docker?一鍵自動(dòng)化部署?SpringBoot?項(xiàng)目的詳細(xì)步驟_docker
- 2022-05-22 C語言鏈接屬性的實(shí)踐應(yīng)用_C 語言
- 2022-09-16 Pandas?類型轉(zhuǎn)換astype()的實(shí)現(xiàn)_python
- 2022-02-20 給定一個(gè)數(shù)組,讓數(shù)組的每一項(xiàng)都乘以2幾種實(shí)現(xiàn)方法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支