C# 使用dataGridView导入导出excel(NPOI),可以通过关键字定位,删除空行等 ...

打印 上一主题 下一主题

主题 624|帖子 624|积分 1872

做下记录,
首先插入一个dataGridView控件,两个button按钮(导入数据,导出数据),一个ComboBox(获取列标题使用),一个textbox(输入关键字),一个定位按钮(定位使用)
1,导入数据(NPOI)
  1. 1   
  2. 2         private void daoRuShuJu_cmd_Click(object sender, EventArgs e)
  3. 3         {
  4. 4             DataTable daNpoi = new DataTable();
  5. 5             string fileName = Application.StartupPath;
  6. 6             fileName += "\\4G模组表.xls";  
  7. 7             string sheetName = "4G模组情况表";
  8. 8             bool isColumnName = true;
  9. 9             IWorkbook workbook;
  10. 10             string fileExt = Path.GetExtension(fileName).ToString();
  11. 11             using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
  12. 12             {
  13. 13                 if (fileExt == ".xlsx")
  14. 14                 {
  15. 15                     workbook = new XSSFWorkbook(fs);
  16. 16                 }
  17. 17                 else if (fileExt == ".xls")
  18. 18                 {
  19. 19                     workbook = new HSSFWorkbook(fs);
  20. 20                 }
  21. 21                 else
  22. 22                 {
  23. 23
  24. 24                     workbook = null;
  25. 25                 }
  26. 26
  27. 27                 ISheet sheet = null;
  28. 28                 if (sheetName != null && sheetName != "")
  29. 29                 {
  30. 30                     sheet = workbook.GetSheet(sheetName);
  31. 31                     if (sheet == null)
  32. 32                     {
  33. 33                         sheet = workbook.GetSheetAt(0);
  34. 34                     }
  35. 35
  36. 36                 }
  37. 37                 else
  38. 38                 {
  39. 39                     sheet = workbook.GetSheetAt(0);
  40. 40                 }
  41. 41
  42. 42                 IRow header = sheet.GetRow(sheet.FirstRowNum);
  43. 43                 int startRow = 0;
  44. 44                 if (isColumnName)
  45. 45                 {
  46. 46                     startRow = sheet.FirstRowNum + 1;
  47. 47                     for (int i = header.FirstCellNum; i < header.LastCellNum; i++)
  48. 48                     {
  49. 49                         ICell cell = header.GetCell(i);
  50. 50                         if (cell != null)
  51. 51                         {
  52. 52                             string cellValue = cell.ToString();
  53. 53                             if (cellValue != null)
  54. 54                             {
  55. 55                                 DataColumn col = new DataColumn(cellValue);
  56. 56                                 daNpoi.Columns.Add(col);
  57. 57                             }
  58. 58                             else
  59. 59                             {
  60. 60                                 DataColumn col = new DataColumn();
  61. 61                                 daNpoi.Columns.Add(col);
  62. 62                             }
  63. 63                         }
  64. 64                     }
  65. 65                 }
  66. 66
  67. 67                 for (int i = startRow; i <= sheet.LastRowNum; i++)
  68. 68                 {
  69. 69                     IRow row = sheet.GetRow(i);
  70. 70                     if (row == null)
  71. 71                     {
  72. 72                         continue;
  73. 73                     }
  74. 74                     DataRow dr = daNpoi.NewRow();
  75. 75                     for (int j = row.FirstCellNum; j < row.LastCellNum; j++)
  76. 76                     {
  77. 77
  78. 78                         if (row.GetCell(j) != null)
  79. 79                         {
  80. 80                             dr[j] = row.GetCell(j).ToString();
  81. 81                         }
  82. 82                     }
  83. 83                     daNpoi.Rows.Add(dr);
  84. 84                 }
  85. 85             }
  86. 86
  87. 87             dataGridView1.DataSource = daNpoi;
  88. 88         }
复制代码
3,删除空行函数
// 删除空白行调用函数,在不需要用户添加新行或者数据的时候使用,ALLowUsertoAddRows 这个属性需要设置false
        //如果ALLowUsertoAddRows 这个属性是true,则,用户可以输入数据,但是row < view.Rows.Count需要   -1不去判断最后一行
// 所以传入的x根据情况更改,想用户可以输入,ALLowUsertoAddRows的值为true,    x=0;
//不想用户输入ALLowUsertoAddRows的值为false    x=1;
  1. 1   private void baoCunShuJu_cmd_Click(object sender, EventArgs e)
  2. 2         {
  3. 3             DataTable dtTable = dataGridView1.DataSource as DataTable;
  4. 4             string sheetName = "4G模组情况表";   //sheet名字
  5. 5             IWorkbook wb = new HSSFWorkbook();
  6. 6             ISheet sheet = string.IsNullOrEmpty(sheetName) ? wb.CreateSheet("sheet1") : wb.CreateSheet(sheetName);
  7. 7             int rowIndex = 0;
  8. 8             if (dtTable.Columns.Count > 0)
  9. 9             {
  10. 10                 IRow header = sheet.CreateRow(rowIndex);
  11. 11                 for (int i = 0; i < dtTable.Columns.Count; i++)
  12. 12                 {
  13. 13                     ICell cell = header.CreateCell(i);
  14. 14                     cell.SetCellValue(dtTable.Columns[i].ColumnName);
  15. 15                 }
  16. 16             }
  17. 17             if (dtTable.Rows.Count > 0)
  18. 18             {
  19. 19                 for (int i = 0; i < dtTable.Rows.Count; i++)
  20. 20                 {
  21. 21                     rowIndex++;
  22. 22                     IRow row = sheet.CreateRow(rowIndex);
  23. 23                     for (int j = 0; j < dtTable.Columns.Count; j++)
  24. 24                     {
  25. 25                         ICell cell = row.CreateCell(j);
  26. 26                         cell.SetCellValue(dtTable.Rows[i][j].ToString());
  27. 27                     }
  28. 28                 }
  29. 29             }
  30. 30
  31. 31             for (int i = 0; i < dtTable.Columns.Count; i++)
  32. 32             {
  33. 33                 sheet.AutoSizeColumn(i);
  34. 34             }
  35. 35
  36. 36             string fileName = Application.StartupPath;      // debug 目录                     
  37. 37             fileName += "\\4G模组表.xls";  //excel 名字
  38. 38             using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  39. 39             {
  40. 40                 wb.Write(fs);
  41. 41             }
  42. 42             MessageBox.Show("保存成功");
  43. 43         }
复制代码
4,获取excel的列标题,给ComboBox控件
  1. 1  private void clearGrid(DataGridView view, int x)
  2. 2         {
  3. 3             for (int row = 0; row < (view.Rows.Count - x); ++row)
  4. 4             {
  5. 5                 bool isEmpty = true;
  6. 6                 for (int col = 0; col < view.Columns.Count; ++col)
  7. 7                 {
  8. 8                     object value = view.Rows[row].Cells[col].Value;
  9. 9                     if (value != null && value.ToString().Length > 0)
  10. 10                     {
  11. 11                         isEmpty = false;
  12. 12                         break;
  13. 13                     }
  14. 14                 }
  15. 15                 if (isEmpty)
  16. 16                 {
  17. 17                     view.Rows.RemoveAt(row--);
  18. 18                 }
  19. 19             }
  20. 20         }
复制代码
 
5,定位,可以在每个列里根据关键字去查找,并且定位到行,可实现下一条功能
  1. 1  public void huoQu_Column()   //把excel表各列标题弄到combobox上
  2. 2         {
  3. 3            // int Rowcount = dataGridView1.RowCount;//获取datagridview的行数
  4. 4             int Columncount = dataGridView1.ColumnCount;//获取datagridview的列数
  5. 5            // dataGridView2.ColumnCount = Columncount;  //新增列
  6. 6            // dataGridView2.ColumnHeadersVisible = true;  //新增的列显示出来
  7. 7             for (int i = 0; i < Columncount; i++)
  8. 8             {
  9. 9                 string var = this.dataGridView1.Columns[i].HeaderText;
  10. 10                // this.dataGridView2.Columns[i].HeaderText = this.dataGridView1.Columns[i].HeaderText;  //2的列名和1的一样
  11. 11               //  dataGridView2.Columns[i].MinimumWidth = dataGridView1.Columns[i].MinimumWidth;   //2的列宽和1 一样
  12. 12                 // = dataGridView1.Rows[0].Cells[i].Value.ToString();
  13. 13                 lie_ming_cb.Items.Add(var);
  14. 14               //  lie_ming_cb_1.Items.Add(var);
  15. 15                // if (var == "IMEI")
  16. 16                // {
  17. 17                    // lie_ming_cb_1.SelectedIndex = i;
  18. 18               //  }
  19. 19             }
  20. 20             lie_ming_cb.SelectedIndex = 0;
  21. 21         }
复制代码
6,textbox可以敲回车直接查找,需要添加textbox的KeyDown事件
  1. 1  int xiayitiao_int = 0;  //下一条标记
  2. 2
  3. 3         private void chaZhao_bt_1_Click(object sender, EventArgs e)   //定位按钮
  4. 4         {
  5. 5             DataTable rentTable = (DataTable)dataGridView1.DataSource;//获取数据源
  6. 6             int r = 0;
  7. 7             bool dingwei_f;  //定位标记,
  8. 8
  9. 9
  10. 10             if ((guanJianZi_box.Text != "") && (guanJianZi_box.Text != "/请输入关键字/"))
  11. 11             {
  12. 12                 for (int i = xiayitiao_int; i < rentTable.Rows.Count; i++)
  13. 13                 {
  14. 14                     dingwei_f = rentTable.Rows[i][lie_ming_cb.Text].ToString().Contains(guanJianZi_box.Text); //对比字符串,
  15. 15
  16. 16                     if (dingwei_f)
  17. 17                     {
  18. 18                         //指定行
  19. 19                         dataGridView1.ClearSelection();
  20. 20                         dataGridView1.Rows[i].Selected = true;
  21. 21                         //让指定行处于选中状态(状态)
  22. 22                         dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[1];
  23. 23                         dataGridView1.CurrentRow.Selected = true;
  24. 24                         dingwei_f = false;
  25. 25                         xiayitiao_int = i+1; //下一条的标记
  26. 26                         r++;
  27. 27                         return;
  28. 28                     }
  29. 29                 }
  30. 30                 if (r > 0)
  31. 31                 {
  32. 32
  33. 33                 }
  34. 34                 else
  35. 35                 {
  36. 36                     MessageBox.Show("没有匹配项或已经是最后一条,将从第一条继续查找", "提示");
  37. 37                     xiayitiao_int = 0;   //重新查找
  38. 38                 }
  39. 39             }
  40. 40             else
  41. 41             {
  42. 42                 MessageBox.Show("请输入正确的关键字", "提示");
  43. 43             }
  44. 44         }
复制代码
 

来源:https://www.cnblogs.com/qi-jiayou/archive/2022/06/17/16385092.html
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大号在练葵花宝典

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表