EXcel 导入数据库和导出数据库的代码
2012-09-19 22:09
369 查看
首先导入NPOI这个程序集
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPOI;//导入的程序集
using Ionic;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOIs.BLL;
using NPOIs.Model;
namespace NPOIs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
导出数据库
private void button1_Click(object sender, EventArgs e)
{
using (FileStream str = new FileStream(@"F:\Book.xls", FileMode.Open, FileAccess.Read))
{
HSSFWorkbook workkbook = new HSSFWorkbook(str);
HSSFSheet sheet = workkbook.GetSheetAt(0) as HSSFSheet;
for (int i = 1; i < sheet.LastRowNum; i++)
{
HSSFRow row = sheet.GetRow(i) as HSSFRow;
TBook book = new TBook();
book.ISBN = row.GetCell(0).StringCellValue;
book.BookName = row.GetCell(1).StringCellValue;
book.Athor = row.GetCell(2).StringCellValue;
book.Publisher = row.GetCell(3).StringCellValue;
book.Price = row.GetCell(4).NumericCellValue;
book.CNum = (int)row.GetCell(5).NumericCellValue;
book.SNum = (int)row.GetCell(6).NumericCellValue;
book.BookPhoto = row.GetCell(7).StringCellValue;
int t = new TBookBLL().AddNew(book);
}
MessageBox.Show("添加成功!");
}
}
//导入数据库
private void button2_Click(object sender, EventArgs e)
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet() as HSSFSheet;
List<TBook> list = new List<TBook>();
list = new TBookBLL().ListAll() as List<TBook>;
for (int i = 0; i < list.Count; i++)
{
HSSFRow row = sheet.CreateRow(i) as HSSFRow;
row.CreateCell(0).SetCellValue(list[i].ISBN);
row.CreateCell(1).SetCellValue(list[i].BookName);
row.CreateCell(2).SetCellValue(list[i].Athor);
row.CreateCell(3).SetCellValue(list[i].Publisher);
row.CreateCell(4).SetCellValue((double)list[i].Price);
row.CreateCell(5).SetCellValue((double)list[i].CNum);
row.CreateCell(6).SetCellValue((double)list[i].SNum);
row.CreateCell(7).SetCellValue(list[i].BookPhoto);
}
FileStream stream = new FileStream(@"F:\TBOOK.xls", FileMode.OpenOrCreate, FileAccess.Write);
workbook.Write(stream);
stream.Dispose();
MessageBox.Show("导入成功!");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPOI;//导入的程序集
using Ionic;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOIs.BLL;
using NPOIs.Model;
namespace NPOIs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
导出数据库
private void button1_Click(object sender, EventArgs e)
{
using (FileStream str = new FileStream(@"F:\Book.xls", FileMode.Open, FileAccess.Read))
{
HSSFWorkbook workkbook = new HSSFWorkbook(str);
HSSFSheet sheet = workkbook.GetSheetAt(0) as HSSFSheet;
for (int i = 1; i < sheet.LastRowNum; i++)
{
HSSFRow row = sheet.GetRow(i) as HSSFRow;
TBook book = new TBook();
book.ISBN = row.GetCell(0).StringCellValue;
book.BookName = row.GetCell(1).StringCellValue;
book.Athor = row.GetCell(2).StringCellValue;
book.Publisher = row.GetCell(3).StringCellValue;
book.Price = row.GetCell(4).NumericCellValue;
book.CNum = (int)row.GetCell(5).NumericCellValue;
book.SNum = (int)row.GetCell(6).NumericCellValue;
book.BookPhoto = row.GetCell(7).StringCellValue;
int t = new TBookBLL().AddNew(book);
}
MessageBox.Show("添加成功!");
}
}
//导入数据库
private void button2_Click(object sender, EventArgs e)
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet() as HSSFSheet;
List<TBook> list = new List<TBook>();
list = new TBookBLL().ListAll() as List<TBook>;
for (int i = 0; i < list.Count; i++)
{
HSSFRow row = sheet.CreateRow(i) as HSSFRow;
row.CreateCell(0).SetCellValue(list[i].ISBN);
row.CreateCell(1).SetCellValue(list[i].BookName);
row.CreateCell(2).SetCellValue(list[i].Athor);
row.CreateCell(3).SetCellValue(list[i].Publisher);
row.CreateCell(4).SetCellValue((double)list[i].Price);
row.CreateCell(5).SetCellValue((double)list[i].CNum);
row.CreateCell(6).SetCellValue((double)list[i].SNum);
row.CreateCell(7).SetCellValue(list[i].BookPhoto);
}
FileStream stream = new FileStream(@"F:\TBOOK.xls", FileMode.OpenOrCreate, FileAccess.Write);
workbook.Write(stream);
stream.Dispose();
MessageBox.Show("导入成功!");
}
}
}
相关文章推荐
- POI 导入导出Excel文件到数据库
- JAVA实现数据库数据导入/导出到Excel(POI技术)
- 数据库导入导出Excel
- Excel表格数据导入导出数据库
- 实现ADO,Excel,Aceess,.NET托管代码见的相互导入导出,利用代码实现数据的相互转换
- C# 读写excel 用于导入数据库 批量导入导出excel
- larave5.6 将Excel文件数据导入数据库代码实例
- C#数据库数据导入导出系列之三 数据库导出到Excel下
- easypoi 一行代码搞定excel导入导出
- SQL.MDB数据库记录Rs导出到Excel.Sheet中代码(VBA+VB.Net)
- 将Excel数据快速大批量导入数据库的代码
- Excel导入导出数据库
- java代码实现excel数据导入数据库
- C#数据库数据导入导出系列之一 ASP.NET Excel导入Sql Server数据库
- C#中对数据库文件的导入导出Excel
- 从excel导入数据到数据库中和gridview导出数据到excel中
- 用Java代码将excel中的数据导入到数据库中
- 导出excel的数据拼接成sql导入到数据库中
- 发一个通用从数据库导出excel、excel导入数据库组件
- PHP将Excel导入数据库及数据库数据导出至Excel的方法