导入
来源:互联网 发布:捷豹系统彩票源码 编辑:程序博客网 时间:2024/04/28 22:15
public ActionResult Admin(HttpPostedFileBase filebase)
{
HttpPostedFileBase file = Request.Files["files"];
string FileName;
string savePath;
if (file == null || file.ContentLength <= 0)
{
ViewBag.error = "文件不能为空";
return View();
}
else
{
string filename = Path.GetFileName(file.FileName);
int filesize = file.ContentLength;//获取上传文件的大小单位为字节byte
string fileEx = System.IO.Path.GetExtension(filename);//获取上传文件的扩展名
string NoFileName = System.IO.Path.GetFileNameWithoutExtension(filename);//获取无扩展名的文件名
int Maxsize = 4000 * 1024;//定义上传文件的最大空间大小为4M
string FileType = ".xls,.xlsx";//定义上传文件的类型字符串
FileName = NoFileName + DateTime.Now.ToString("yyyyMMddhhmmss") + fileEx;
if (!FileType.Contains(fileEx))
{
ViewBag.error = "文件类型不对,只能导入xls和xlsx格式的文件";
return View();
}
if (filesize >= Maxsize)
{
ViewBag.error = "上传文件超过4M,不能上传";
return View();
}
string path = AppDomain.CurrentDomain.BaseDirectory + "Excel/";
savePath = Path.Combine(path, FileName);
file.SaveAs(savePath);
}
//string result = string.Empty;
string strConn;
strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + savePath + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
try
{
myCommand.Fill(myDataSet, "ExcelInfo");
}
catch (Exception ex)
{
ViewBag.error = ex.Message;
return View();
}
DataTable table = myDataSet.Tables["ExcelInfo"].DefaultView.ToTable();
//引用事务机制,出错时,事物回滚
using (TransactionScope transaction = new TransactionScope())
{
for (int i = 0; i < table.Rows.Count; i++)
{
////获取地区名称
//string _areaName = table.Rows[i][0].ToString();
////判断地区是否存在
//if (!_areaRepository.CheckAreaExist(_areaName))
//{
// ViewBag.error = "导入的文件中:" + _areaName + "地区不存在,请先添加该地区";
// return View();
//}
//else
//{
// Station station = new Station();
// station.AreaID = _areaRepository.GetIdByAreaName(_areaName).AreaID;
// station.StationName = table.Rows[i][1].ToString();
// station.TerminaAddress = table.Rows[i][2].ToString();
// station.CapacityGrade = table.Rows[i][3].ToString();
// station.OilEngineCapacity = decimal.Parse(table.Rows[i][4].ToString());
// _stationRepository.AddStation(station);
//}
//循环保存
LeBlog.Service.UserService server = new UserService();
UserInfo uif = new UserInfo();
uif.UserName = table.Rows[i][0].ToString();
uif.RealName = table.Rows[i][1].ToString();
uif.Password = table.Rows[i][2].ToString();
uif.Autograph = table.Rows[i][3].ToString();
if (server.Regist(uif))
{
transaction.Complete();
}
}
}
ViewBag.error = "导入成功";
System.Threading.Thread.Sleep(2000);
return Content("<script>alert('数据导入成功!');location.href='/Home/Index'</script>");
}
}
- 导入
- 导入
- 导入
- 导入
- 导入
- 导入
- 导入
- 导入和静态导入
- sqlplus导入
- 导入本地
- EXCEL导入
- 导入导出
- 导入Eaxl
- 导入EXCEL
- 导入/导出
- oracle导入
- 导入数据库
- Excel 导入
- 关于svn在androidstudio中配置的问题
- 6款漂亮HTML CSS样式用户留言表单
- hdu5877 dfs+线段树离散化
- Android中Java 与 JavaScript交互
- 如何使用分布是缓存Hazelcast
- 导入
- HDU5015-233 Matrix
- Bzoj 1923: [Sdoi2010]外星千足虫(bitset优化高斯消元)
- HDU 5869 求区间中不同连续序列的gcd的个数(树状数组)
- 继承
- codeforces 580D
- Unity3D 世界坐标上一个点对应UI上一个点
- ConcurrentHashMap的putIfAbsent可能存在的问题
- 文件目录常用操作的类