使用C#对Excel进行读写操作(NPOI)以及使用EF对Mysql进行CURD

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.HSSF.Record.Formula.Functions;


namespace MysqlWinform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void skinButton1_Click(object sender, EventArgs e)
        {
            //using (FileStream fs = new FileStream(@"C:\Users\Administrator\Desktop\Excel\goodstype1.xls", FileMode.Open, FileAccess.Read))
            //{
            //    HSSFWorkbook work = new HSSFWorkbook(fs);
            //    HSSFSheet sheet = work.GetSheetAt(0);
            //    HSSFRow row = null;
            //    goodstype gt = null;
            //    zlyfmallEntities db = new zlyfmallEntities();
            //    for (int i = 1; i <= sheet.LastRowNum; i++)
            //    {
            //        row = sheet.GetRow(i);
            //        gt = new goodstype();
            //        gt.tname = row.GetCell(0).ToString();
            //        gt.status = int.Parse(row.GetCell(1).ToString());
            //        db.goodstype.Add(gt);
            //    }
            //    db.SaveChanges();
            //}
        }


        private void skinButton2_Click(object sender, EventArgs e)
        {
            using (FileStream fs = new FileStream(@"c:\users\administrator\desktop\excel\goods_classone.xls", FileMode.Open, FileAccess.Read))
            {
                HSSFWorkbook work = new HSSFWorkbook(fs);
                HSSFSheet sheet = work.GetSheetAt(0);
                zlyfmallEntities2 db = new zlyfmallEntities2();
                HSSFRow row = null;
                goodsclasstwo gco = null;




                for (int i = 1; i <= 95; i++)
                {
                    row = sheet.GetRow(i);
                    gco = new goodsclasstwo();
                    gco.tname = row.GetCell(0).ToString();
                    gco.father = int.Parse(row.GetCell(1).ToString());
                    gco.status = 1;
                    db.goodsclasstwo.Add(gco);
                }


                db.SaveChanges();
                MessageBox.Show("插入成功");


            }
        }


        private void skinButton1_Click_1(object sender, EventArgs e)
        {
            
        }


        private void skinButton1_Click_2(object sender, EventArgs e)
        {


            int m = 0;
            
            using (FileStream fsread = new FileStream(@"C:\Users\Administrator\Desktop\Excel\zong.xls", FileMode.Open, FileAccess.Read))
            {
                using (FileStream fswrite = new FileStream(@"C:\Users\Administrator\Desktop\Excel\goodsClassThree.xls", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    HSSFWorkbook workread = new HSSFWorkbook(fsread);
                    HSSFSheet sheetread = workread.GetSheetAt(0);


                    HSSFWorkbook workwrite = new HSSFWorkbook();
                    HSSFSheet sheetwrite = workwrite.CreateSheet("goodsClassOne");
                    for (int i = 0; i <= sheetread.LastRowNum; i++)
                    {
                        HSSFRow row = sheetread.GetRow(i);
                        for (int j = 0; j <= row.LastCellNum; j++)
                        {
                            HSSFCell cell = row.GetCell(j);
                            if (cell != null)
                            {
                                if (cell.ToString() != "")
                                {
                                    HSSFRow newrow = sheetwrite.CreateRow(m);
                                    newrow.CreateCell(0, HSSFCellType.STRING).SetCellValue(cell.ToString());
                                    newrow.CreateCell(1, HSSFCellType.NUMERIC).SetCellValue(i+1);
                                    m++;
                                }
                            }
                        }
                    }
                    workwrite.Write(fswrite);


                }
               
                MessageBox.Show("写入成功共" + m);
                
            }
        }


        private void skinButton3_Click(object sender, EventArgs e)
        {
            using (FileStream fs = new FileStream(@"C:\Users\Administrator\Desktop\Excel\goodsClassThree.xls", FileMode.Open, FileAccess.Read))
            {
                HSSFWorkbook wk = new HSSFWorkbook(fs);
                HSSFSheet sheet = wk.GetSheetAt(0);
                HSSFRow row = null;
                goodsclassthree gc = null;
                zlyfmallEntities2 db = new zlyfmallEntities2();
                for (int i = 0; i <= sheet.LastRowNum; i++)
                {
                    row = sheet.GetRow(i);
                    gc = new goodsclassthree();
                    gc.tname = row.GetCell(0).ToString();
                    gc.father = int.Parse(row.GetCell(1).ToString());
                    gc.status = 1;
                    db.goodsclassthree.Add(gc);
                }
                db.SaveChanges();
                MessageBox.Show("插入成功");
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            using (FileStream fs = new FileStream(@"C:\Users\Administrator\Desktop\Excel\goodstype1.xls", FileMode.Open, FileAccess.Read))
            {
                HSSFWorkbook work = new HSSFWorkbook(fs);
                HSSFSheet sheet = work.GetSheetAt(0);
                HSSFRow row = null;
                goodsclassone gt = null;
                zlyfmallEntities2 db = new zlyfmallEntities2();
                for (int i = 1; i <= sheet.LastRowNum; i++)
                {
                    row = sheet.GetRow(i);
                    gt = new goodsclassone();


                    gt.tname = row.GetCell(0).ToString().Replace(" ", ",");
                    gt.status = int.Parse(row.GetCell(1).ToString());
                    db.goodsclassone.Add(gt);
                }
                db.SaveChanges();
            }
        }
    }
}

你可能感兴趣的:(c#,Mysql,Excel,数据库,C#,Mysql,Excel,NPOI)