winform使用NPIO导入导出Excel

  1. 安装包NPIOwinform使用NPIO导入导出Excel_第1张图片

  2. 命名空间
    using NPOI.SS.UserModel;
    using NPOI.XSSF.UserModel;
    using System.IO;

  3. 界面
    winform使用NPIO导入导出Excel_第2张图片

  4. 导出

 /// 
        /// 导出
        /// 
        /// 
        /// 
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            //实例化XSSF
            XSSFWorkbook workBook = new XSSFWorkbook();
            //创建一个sheet
            XSSFSheet sheet = (XSSFSheet)workBook.CreateSheet();
            // 添加表头
            IRow row = sheet.CreateRow(0);

            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                row.CreateCell(i).SetCellValue(dataGridView1.Columns[i].HeaderText);
            }

            //row.CreateCell(0).SetCellValue("ID");
            //row.CreateCell(1).SetCellValue("姓名");
            //row.CreateCell(2).SetCellValue("年龄");

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                IRow rrow = sheet.CreateRow(i + 1);

                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    rrow.CreateCell(j).SetCellValue(dataGridView1.Rows[i].Cells[j].Value.ToString());
                }
                //rrow.CreateCell(0).SetCellValue(dataGridView1.Rows[i].Cells["id"].Value.ToString());
                //rrow.CreateCell(1).SetCellValue(dataGridView1.Rows[i].Cells["name"].Value.ToString());
                //rrow.CreateCell(2).SetCellValue(dataGridView1.Rows[i].Cells["age"].Value.ToString());
            }

            SaveFileDialog open = new SaveFileDialog();
            open.Filter = "Excel文件|*.xlsx;*.xls|Excel文件|*.xls|Excel文件|*.xlsx"; ;
            open.Title = "保存文件";

            if (open.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(open.FileName, FileMode.Create, FileAccess.Write))
                {
                    workBook.Write(fs);
                    workBook.Close();
                }
                MessageBox.Show("导出成功");
            }
        }
  1. 导入
/// 
        /// 导入
        /// 
        /// 
        /// 
        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            // 打开文件保存对话框
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel文件|*.xlsx;*.xls";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;
                DataTable dt = new DataTable();
                using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    // 创建Excel文件
                    IWorkbook workbook = WorkbookFactory.Create(fileStream);
                    // 获取第一个工作表
                    ISheet sheet = workbook.GetSheetAt(0);
                    // 获取第一个工作表头
                    IRow headerRow = sheet.GetRow(0);
                    //便利表头
                    foreach (ICell cell in headerRow)
                    {
                        dt.Columns.Add(cell.ToString());
                    }
                    //便利表数据
                    for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        DataRow dataRow = dt.NewRow();
                        for (int j = 0; j < row.LastCellNum; j++)
                        {
                            ICell cell = row.GetCell(j);
                            if (cell != null)
                            {
                                dataRow[j] = cell.ToString();
                            }
                        }
                        dt.Rows.Add(dataRow);
                    }
                }
                dataGridView1.DataSource = dt;
            }
        }

你可能感兴趣的:(excel,c#)