Asp.net 模板下载和导入到DataTable中
HTML页面:
<tr>
<td colspan="" style="text-align: left; border: 1px;">
<asp:FileUpload ID="FileUpload1" runat="server" CssClass="dfFile" Width="350px" />
<asp:Button ID="btnImport" runat="server" Text="导入" CssClass="scbtn" OnClientClick="return LoadFile();"
OnClick="btnImport_Click" />
<asp:Button ID="btnDownLoad" runat="server" Text="模板下载" CssClass="scbtn" OnClick="btnDownLoad_Click" />
</td>
</tr>
Asp.net后台:
#region 导入
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnImport_Click(object sender, EventArgs e)
{
string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//获得文件的扩展名
if (IsXls != ".xls" && IsXls != ".xlsx")
{
ShowMessageBox("只可以选择Excel文件");
return;
}
HttpPostedFile file = this.FileUpload1.PostedFile; string filename = FileUpload1.FileName; //获取Execle文件名
string filePath = Server.MapPath("../StorageCheck/CheckUpFiles/" + FileUpload1.FileName);
file.SaveAs(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312"));
try
{
DataSet ds = ExcelSqlConnection(filePath, filename); //调用自定义方法
DataTable dt = ds.Tables[]; //循环移除DataTable中的空行
List<DataRow> removelist = new List<DataRow>();
for (int i = ; i < dt.Rows.Count; i++)
{
bool rowIsNull = true; //标记是否有空行
for (int j = ; j < dt.Columns.Count; j++)
{
if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString().Trim()))
{ rowIsNull = false;
}
}
if (rowIsNull)
{
removelist.Add(dt.Rows[i]);
}
}
for (int i = ; i < removelist.Count; i++)
{
dt.Rows.Remove(removelist[i]);
} string detName = "";
string detId = "";
if (dt != null && dt.Rows.Count > )
{
foreach (DataRow dr in dt.Rows)
{
detName += dr["箱号"].ToString() + "[" + dr["盒号"].ToString() + "]" + "[" + dr["起始卡号"].ToString() + "]" + "[" + dr["结束卡号"].ToString() + "]" +
"[" + dr["应有数量/张"].ToString() + "]" + "[" + dr["实际数量/张"].ToString() + "]" + "[" + dr["缺卡卡号"].ToString() + "];";
detId += dr["箱号"].ToString() + "," + dr["盒号"].ToString() + "," + dr["起始卡号"].ToString() + "," + dr["结束卡号"].ToString() + "," +
dr["应有数量/张"].ToString() + "," + dr["实际数量/张"].ToString() + "," + dr["缺卡卡号"].ToString(); if (string.IsNullOrEmpty(dr["缺卡卡号"].ToString()))
{
detId += "" + ";";
}
}
hf_CheckId.Value = detId.TrimEnd(';');
}
}
catch
{ }
finally
{
fs.Flush(); //释放流
fs.Close();//关闭流
}
}
#endregion #region 模板下载
/// <summary>
/// 模板下载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDownLoad_Click(object sender, EventArgs e)
{
//filePath为文件在服务器上的地址
string excelFile = Server.MapPath("../StorageCheck/DownLoad/白卡入库导入模板.xls");
FileInfo fi = new FileInfo(excelFile);
HttpResponse contextResponse = HttpContext.Current.Response;
contextResponse.Clear();
contextResponse.Buffer = true;
contextResponse.Charset = "GB2312"; //设置类型
contextResponse.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", excelFile)); //定义输出文件和文件名
contextResponse.AppendHeader("Content-Length", fi.Length.ToString());
contextResponse.ContentEncoding = Encoding.Default;
contextResponse.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 contextResponse.WriteFile(fi.FullName);
contextResponse.Flush();
contextResponse.End();
}
#endregion #region 读取Excel数据
/// <summary>
/// 读取Excel数据
/// </summary>
/// <param name="filepath">Excel服务器路径</param>
/// <param name="tableName">Excel表名称</param>
/// <returns></returns>
public static System.Data.DataSet ExcelSqlConnection(string filepath, string tableName)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection ExcelConn = new OleDbConnection(strCon);
try
{
string strCom = string.Format("SELECT * FROM [Sheet1$]");
//string strCom = "select * from [" + tableName + "]";
ExcelConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[" + tableName + "$]");
ExcelConn.Close();
return ds;
}
catch
{
ExcelConn.Close();
return null;
}
}
#endregion
Asp.net 模板下载和导入到DataTable中的更多相关文章
- SVN下载项目导入到eclipse中出现错误解决办法:
首先要确定settings.xml配置路径正确 (下面是我自己的路径,设置自己的路径) 用客户端暴力解决方法: 1)把本地中工作空间中内容删除重新下载 2)导入到eclipse中 会出现一些问题 右键 ...
- Excel 导入到Datatable 中,再使用常规方法写入数据库
首先呢?要看你的电脑的office版本,我的是office 2013 .为了使用oledb程序,需要安装一个引擎.名字为AccessDatabaseEngine.exe.这里不过多介绍了哦.它的数据库 ...
- Asp.net 修改已有数据的DataTable中某列的数据类型
DataTable dt_PI = new DataTable(); //克隆表结构 dt_PI = ds.Tables[].Clone(); dt_PI.Columns["FLTFullP ...
- 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
using System; using System.Collections.Generic; using System.Text; using System.IO; using NPOI.SS.Us ...
- winfrom 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
1.通过NUGET管理器下载nopi,在引入命令空间 using System; using System.Collections.Generic; using System.Text; using ...
- Excel导入到DataTable ,DataTable导入到Excel
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS. ...
- 使用OpenXML将Excel内容读取到DataTable中
前言:前面的几篇文章简单的介绍了如何使用OpenXML创建Excel文档.由于在平时的工作中需要经常使用到Excel的读写操作,简单的介绍下使用 OpenXML读取Excel中得数据.当然使用Open ...
- SpringBoot集成MongoDB之导入导出和模板下载
前言 自己很对自己在项目中集成MongoDb做的导入导出以及模板下载的方法总结如下,有不到之处敬请批评指正! 1.pom.xml依赖引入 <!-- excel导入导出 --> <de ...
- Spring1:Spring简介、环境搭建、源码下载及导入MyEclipse
框架学习前言 这个模块是面向Spring的,Spring的学习我是这么想的: 1.简单介绍Spring,主要是从网上借鉴一些重点 2.尽量说明清楚Spring的使用方法以及细节点 3.尽量以自己的理解 ...
随机推荐
- js判断设备类型
1. 判断微信 function is_weixin() { var ua = window.navigator.userAgent.toLowerCase(); if (ua.match(/Micr ...
- 牛客练习赛42 出题的诀窍(数学+hash)
出题的诀窍 题目链接:https://ac.nowcoder.com/acm/contest/393/C 题解: 由于他是在每一行选取一个元素,然后纵向来比较,这里行的顺序是不会影响的,所以我们将每一 ...
- layer 限定时间消失
默认tips层几秒后自动关闭.请问如何实现类似页面层时,点击页面层外部遮罩手动关闭的效果? 下面我加了time: 20000 时间控制在20秒后自动关闭,但是显得比较呆板,不如用户手动点击关闭来的灵活 ...
- ZooKeeper配额指南(十)
配额 ZK有命名空间和字节配额.你可以使用ZooKeeperMain类来设置配额.ZK打印警告信息如果用户超过分配给他们的配额.这些信息被打印到ZK的日志中. $java -cp zookeeper. ...
- 51Nod 1062 序列中最大的数 | 简单DP
#include "iostream" #include "cstdio" using namespace std; #define LL long long ...
- Linux 下访问Windows共享目录
5.安装cifs-utils软件包 # yum install cifs-utils 貌似可以不用安装这个包. 6.在Linux下创建一个挂载点 # mkdir /mnt/Windows 7.挂载W ...
- 【GDKOI2016Day1T1-魔卡少女】【拆位】线段树维护区间内所有连续子区间的异或和
题意:给出N个数,M个操作.操作有修改和询问两种,每次修改将一个数改成另一个数,每次询问一个区间的所有连续子区间的异或和.n,m<=100000,ai<=1000 题解: 当年(其实也就是 ...
- 20155335《java程序设计》第一周学习总结
18个章节的问题 (1)为什么需要JVM让java跨平台? (2)JVM与JDK,与JRE的关系? (3)为什么 -0/3 结果是 0,而 -0.0/3.0 结果是 -0.0?(注意后边的结果0带负号 ...
- R、Python、Scala和Java,到底该使用哪一种大数据编程语言?
有一个大数据项目,你知道问题领域(problem domain),也知道使用什么基础设施,甚至可能已决定使用哪种框架来处理所有这些数据,但是有一个决定迟迟未能做出:我该选择哪种语言?(或者可能更有针对 ...
- poj 2000 Gold Coins
题目链接:http://poj.org/problem?id=2000 题目大意:求N天得到多少个金币,第一天得到1个,第二.三天得到2个,第四.五.六天得到3个....以此类推,得到第N天的金币数. ...