码迷,mamicode.com
首页 > Windows程序 > 详细

c# 导入

时间:2016-05-18 12:40:02      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ImportFiles.aspx.cs" Inherits="DataReport.OilDistribution.ImportFiles1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">    

<title>导入</title>    

<script src="../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>    

<script src="../Scripts/boot.js" type="text/javascript"></script>    

<script src="../Scripts/Utility.js" type="text/javascript"></script>    

<script src="swfupload/swfupload.js" type="text/javascript">

</script>    

<style type="text/css">        

body         {             font-size: 12px;         }    

</style>

</head>

<body>    

<table border="0" cellpadding="0" cellspacing="0" align="center" width="90%">        

<tr>            

<td colspan="2" height="5px">            

</td>        

</tr>        

<tr>            

<td align="right" width="25%" height="40px">                

选择文件:&nbsp;&nbsp;            

</td>            

<td>                

<input id="fileupload1" style="width: 90%;" class="mini-fileupload" name="Fdata"                     limittype="*.xls;*.xlsx" flashurl="../OilDistribution/swfupload/swfupload.swf"                     onuploadsuccess="onUploadSuccess" />            

</td>        

</tr>        

<tr>            

<td height="40px">                 &nbsp;            

</td>            

<td>                

<a class="mini-button" id="btnImport" onclick="Import()" style="width: 60px;">确定</a>&nbsp;&nbsp;                

<a class="mini-button" id="btnCancel" onclick="onCancel()" style="width: 60px;">取消</a>          

   </td>      

   </tr>   

  </table>

</body>

 

 

<script type="text/javascript">   

mini.parse();
    var fileupload = mini.get("fileupload1");
    var date = Utility.getQueryString("date");
    var url = bootPATH + "../OilDistribution/AjaxReport.ashx?method=Import";
    fileupload.uploadUrl = url;
    //文件导入
    function Import() {
        if (fileupload.getText() == "") {
            mini.alert("请选择要导入的excel文件", "信息提示");
            return;
        }
        fileupload.startUpload();
    }
    function onUploadSuccess(e) {
        switch (e.serverData) {
            case "suc":
                mini.alert("导入成功", "信息提示", function () {
                    CloseWindow("cancel");
                });
                break;
            case "a":
                mini.alert("没有需要导入的数据", "信息提示", function () {
                    CloseWindow("cancel");
                });
                break;
            case "b":
                mini.alert("导入的文件不存在", "信息提示", function () {
                    CloseWindow("cancel");
                });
                break;
            case "p":
                mini.alert("不能确认你要上传哪个省的数据", "信息提示", function () {
                    CloseWindow("cancel");
                });
                break;
            case "exists":
                mini.alert("日报已审核,请联系管理员取消审核后再重新上报", "信息提示", function () {
                    CloseWindow("cancel");
                });
                break;
            default:
                mini.alert("导入失败", "信息提示");
                break;
        }
        this.setText("");
    }
    //关闭弹出窗口
    function CloseWindow(action) {
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();
    }
    function onCancel(e) {
        CloseWindow("cancel");
    }

</script>
</html>

 

 

 

 protected override object processQuery(HttpContext context, out bool hasError)
        {
            hasError = false;
            string method = context.Request["method"];
            switch (method)
            { case "Import": //导入
                    return Import(context, out hasError);

   }
        }

 

 

 

 

 

 

 

 

     private string Import(HttpContext context, out bool hasError)        

{            

ReportState32BLL sbll = new ReportState32BLL();            

hasError = false;            

string root = context.Server.MapPath("~/UploadFiles/");           

  //找到目标文件对象            

HttpPostedFile uploadFile = context.Request.Files["Fdata"];           

  //获取文件后缀           

  string ext = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf("."));          

   string fileName = uploadFile.FileName;            

string file = string.Format("{0}{1}{2}{3}", root, Sessions.CurrentUser.ID, DateTime.Now.ToString("yyyyMMddhhmmssffff"), ext);            

string sheetName = "成品油进、销、存日报";           

  uploadFile.SaveAs(file);            

try             {                

//进销存日报主表赋值              

   Sys_ReportState32 model = new Sys_ReportState32();                

string proid = Sessions.CurrentUser.ProvinceID;                

if (proid == null || proid == "")                

{                    

return "p";//没有需要导入的数据               

  }                

model.Id = Convert.ToInt32(DateTime.Now.ToString("yyMMdd") + proid);                

model.ProvinceID = Convert.ToInt32(proid);               

  model.UserID = Sessions.CurrentUser.ID;                

model.RecordStatus = 2;//  1未上报    2上报未审核   3审核通过                

model.ReportDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));             

    model.FILEPATH = file;               

  int state = sbll.GetStateById(model.Id);          

       if (state != 3) // 3 审核通过              

   {                    

if (File.Exists(file))                    

{                       

  ExcelDataHelper excelDataHelper = new ExcelDataHelper(file);                       

  DataTable dt = excelDataHelper.GetAllData(sheetName); //获取excel数据                        

if (dt == null)                         {                          

   return excelDataHelper.ErrorMessage;                        

}                        

else                        

{                         

    if (dt.Rows.Count == 0)                            

{                               

  return "a"; //没有需要导入的数据                            

}                            

//导入数据                           

  bool result = sbll.Import(dt, model);                            

if (result)                            

{                               

  return "suc";                            

}                        

}                    

}                    

else                    

{                       

  return "b"; //导入的文件不存在                    

}                

}                

else              

   {                    

return "exists";                

}

           

}            

catch (Exception ex)            

{               

  context.Response.Write("<script Language=‘JavaScript‘>...alert(" + ex + ")</script>");            

     return "no";

 

            }            

return "";

        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

using System.Data.OleDb;

namespace CNPC.Platform.Utility {    

public class ExcelDataHelper     {        

public string ErrorMessage { get; set; }

        string connStr = string.Empty;

        public ExcelDataHelper(string xlsFile)        

{             connStr = getConnStr(xlsFile);        

}

        public DataTable GetAllData(string sheetName)        

{           

  ErrorMessage = string.Empty;          

   DataTable dt = null;           

  string sql = string.Format("select * from [{0}$]", sheetName);         

    OleDbDataAdapter oleDbAdapter = null;        

     try             {             

    oleDbAdapter = new OleDbDataAdapter(sql, connStr);          

       dt = new DataTable();             

    oleDbAdapter.Fill(dt);     

            dt.TableName = sheetName;            

}           

  catch (Exception ex)            

{               

  dt = null;   

             

ErrorMessage = ex.Message;           

  }          

   finally        

     {              

   if (oleDbAdapter != null)              

   {                  

   oleDbAdapter.Dispose();             

        oleDbAdapter = null;        

         }         

    }            

return dt;        

}

        string getConnStr(string xlsFile)         {        

     string ext = xlsFile.Substring(xlsFile.LastIndexOf(‘.‘) + 1).ToLower();      

       switch (ext)            

{                

case "xlsx":                    

return string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= {0};Extended Properties=‘Excel 12.0;HDR=YES;IMEX=1;‘", xlsFile);              

   case "xls":                

default:                    

return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=‘Excel 8.0;HDR=YES;IMEX=1‘", xlsFile);

            }        

}    

}

}

 

c# 导入

标签:

原文地址:http://www.cnblogs.com/lk516924/p/5504547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!