zoukankan      html  css  js  c++  java
  • 一种简单的导出导入希望大神别介意

    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();

  • 相关阅读:
    使用SQL Server Management Studio 创建数据库备份作业
    ClickOnce 获取客户端发布版本号
    在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
    找不到方法:“Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)
    常用操作类
    数据库命名规范
    expression动态构成
    C# 获得当前方法 和 方法调用链 的 方法
    EF架构封装类
    基于微软企业库的分层代码框架
  • 原文地址:https://www.cnblogs.com/xiaojian1/p/5465438.html
Copyright © 2011-2022 走看看