C#中对数据库文件的导入导出Excel

Excel导入数据库

using (Stream stream = File.Open(@"E:\10级net3班信息表.xls", FileMode.Open, FileAccess.Read))//建立流,指向要读取的文件
            {
                HSSFWorkbook workbook = new HSSFWorkbook(stream);
                HSSFSheet sheet = workbook.GetSheet("10级net3班联系表");
                excelthreelayer.bll.T_classmessage bstudent = new excelthreelayer.bll.T_classmessage();

                //遍历所有行,LastRowNum是最后一行的索引,而不是总的行数,总的行数应该是LastRowNum+1,所以下面使用i<=sheet.LastRowNum或者i<LastRowNum+1
                List<excelthreelayer.model.T_classmessage> list = new List<excelthreelayer.model.T_classmessage>();
                for (int i = 1; i <= sheet.LastRowNum; i++)
                {
                         HSSFRow row = sheet.GetRow(i);//获取行
                        excelthreelayer.model.T_classmessage minfo = new excelthreelayer.model.T_classmessage();
                        minfo.classname = row.GetCell(0).StringCellValue;
                        minfo.userid = row.GetCell(1).StringCellValue;
                        minfo.username = row.GetCell(2).StringCellValue;
                        minfo.tel = row.GetCell(3).StringCellValue;
                        minfo.QQ = row.GetCell(4).StringCellValue;
                        list.Add(minfo);
                                     

                }
                bstudent.Add1(list);
                MessageBox.Show("操作成功");
            }

将数据库的表信息导入Excel文件

  excelthreelayer.bll.T_classmessage b = new excelthreelayer.bll.T_classmessage();
            int maxid = b.GetMaxId();
            using (FileStream stream = new FileStream(@"d:\2010级net班信息表.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite))//建立文件写入流
            {
                HSSFWorkbook workbook = new HSSFWorkbook();
               
                HSSFSheet sheet = workbook.CreateSheet("net班信息表");
                sheet.CreateRow(0).CreateCell(0).SetCellValue("编号");//添加表头
                sheet.CreateRow(0).CreateCell(1).SetCellValue("班级名称");
                sheet.CreateRow(0).CreateCell(2).SetCellValue("学号");
                sheet.CreateRow(0).CreateCell(3).SetCellValue("姓名");
                sheet.CreateRow(0).CreateCell(4).SetCellValue("电话");
                sheet.CreateRow(0).CreateCell(5).SetCellValue("QQ");

                for (int i = 1; i < maxid; i++)
                {
                   excelthreelayer.model.T_classmessage model = b.GetModel(i);//利用方法得到数据行的数据
                    HSSFRow row = sheet.CreateRow(i);
                    row.CreateCell(0, HSSFCell.CELL_TYPE_STRING).SetCellValue(model.id);
                    row.CreateCell(1, HSSFCell.CELL_TYPE_STRING).SetCellValue(model.classname);
                    row.CreateCell(2, HSSFCell.CELL_TYPE_STRING).SetCellValue(model.userid);
                    row.CreateCell(3, HSSFCell.CELL_TYPE_STRING).SetCellValue(model.username);
                    row.CreateCell(4, HSSFCell.CELL_TYPE_STRING).SetCellValue(model.tel);
                    row.CreateCell(5, HSSFCell.CELL_TYPE_STRING).SetCellValue(model.QQ);
                   
                }
                workbook.Write(stream);//写入文件流
            }
           

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值