一种简单的导出导入希望大神别介意

FileStream fsRead = File.OpenRead(Server.MapPath("~/daochu.xls"));
IWorkbook wk = new HSSFWorkbook(fsRead);
ISheet sheet = wk.GetSheetAt(0);
string sql = "insert into news values(@title,@leibie,@neiro,@date,@didian,@tupian,1,1)";
for (int r = 0; r <= sheet.LastRowNum; r++)
{
SqlConnection conn = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand(sql, conn);
//SqlConnection conn = new SqlConnection(conStr);
// string sql = "insert into news values(@title,@leibie,@neiro,@date,@didian,@tupian,1,1)";
//SqlCommand cmd = new SqlCommand(sql, conn);
IRow row = sheet.GetRow(r);
string title = row.GetCell(0).StringCellValue;
var leibie = row.GetCell(1).NumericCellValue;
string neiro = row.GetCell(2).StringCellValue;
DateTime date = row.GetCell(3).DateCellValue;
string didian = row.GetCell(4).StringCellValue;
string tupian = row.GetCell(5).StringCellValue;
//string sql = "insert into news(newstitle,categoryId,newscontent,newsCreateTime,newssubmitArea,newsReourceId,newsShenHe,newsrManageId) values('" + title + "'," + leibie + ",'" + neiro + "','" + date + "','" + didian + "','" + tupian + "',1,1)";
//SqlCommand cmd = new SqlCommand(sql, conn)
SqlParameter[] proc = new SqlParameter[]
{
new SqlParameter("@title",title),
new SqlParameter("@leibie",leibie),
new SqlParameter("@neiro",neiro),
new SqlParameter("date",date),
new SqlParameter("@didian",didian),
new SqlParameter("@tupian",tupian)
};
cmd.Parameters.AddRange(proc);
conn.Open();
int c = cmd.ExecuteNonQuery();
if (c > 0)
{
Response.Write("<script>alert('导出成功')</script>");
}
else
{
Response.Write("No");
}
conn.Close();

SqlConnection sqlCon = new SqlConnection(conStr);
string sql="SELECT newsID,newstitle,category.categoryname,newscontent,newsCreateTime,newssubmitArea,newsReourceId FROM news as ht INNER JOIN category on category.categoryId=ht.categoryId";
SqlCommand cmd = new SqlCommand(sql, sqlCon);
sqlCon.Open();
SqlDataReader panduan = cmd.ExecuteReader();
if(panduan.HasRows)
{
IWorkbook wk = new HSSFWorkbook();
ISheet sheet = wk.CreateSheet("news");
int rowindex = 0;
while(panduan.Read())
{
int autoId = panduan.GetInt32(0);
string title = panduan.GetString(1);
string categoryId = panduan.GetString(2);
string newscontent = panduan.GetString(3);
DateTime newsCreateTime = panduan.GetDateTime(4);
string newssubmitArea = panduan.GetString(5);
string newsReourceId = panduan.GetString(6);
IRow row = sheet.CreateRow(rowindex);
rowindex++;
row.CreateCell(0).SetCellValue(autoId);
row.CreateCell(1).SetCellValue(title);
row.CreateCell(2).SetCellValue(categoryId);
row.CreateCell(3).SetCellValue(newscontent);
row.CreateCell(4).SetCellValue(newsCreateTime);
row.CreateCell(5).SetCellValue(newssubmitArea);
row.CreateCell(6).SetCellValue(newsReourceId);
}
FileStream fswite = File.OpenWrite("E:\第三个资料1\项目实战\daoru.xls");
wk.Write(fswite);
Response.Write("<script>alert('导入成功')</script>");
fswite.Close();
}
sqlCon.Close();

原文地址:https://www.cnblogs.com/xiaojian1/p/5465438.html