导入xls文档格式如下
序号 | 工号 | 姓名 |
---|---|---|
1 | XXXXX1 | 张三 |
2 | XXXXX2 | 李四 |
前端代码
<tr>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" Width="246px" />
<asp:Button ID="btnUpload" runat="server" Text="上传档案"
onclick="btnUpload_Click" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSure" runat="server" Enabled="false" Text="转入" onclick="btnSure_Click" />
</td>
</tr>
//这种样式也可以,不作为下面的参考
<td>
<input id="upfile" style="width: 300px" type="file" name="upfile" runat="server" /><font face="新細明體"> </font>
<asp:Button ID="upload" runat="server" Text="上傳" onclick="upload_Click" />
<asp:Button ID="Button_C" runat="server" Text="取消" onclick="Button_C_Click" />
</td>
后端代码
上传文件
protected void btnUpload_Click(object sender, EventArgs e)
{
if (this.FileUpload1.PostedFile.ContentLength == 0)
{
this.Literal1.Text = "<script>alert('上传文件不能为空!');</script>";
}
else
{
if (this.FileUpload1.FileName.IndexOf(".xls") > 0)
{
//检查上传资料,有错则删除服务器上的文件
string path = Server.MapPath("../uploadfile/");
string filename = DateTime.Now.ToString("yyyyMMddhhmmss")+"_" + this.FileUpload1.FileName;
path += filename;
if (File.Exists(path) == false)
{
this.FileUpload1.PostedFile.SaveAs(path);
ViewState["filepath"] = path;//有效期等于页面的生存期
//调用将xls文件转为DataSet的方法
DataSet ds = ExcelSqlConnection(path);
DataRow[] dr = ds.Tables[0].Select();
int rowsnum = ds.Tables[0].Rows.Count;
if(CheckError(dr) == false) //CheckError()为自写检查excel内容是否正常方法
{
this.FileUpload1.Dispose();
File.Delete(path);
this.btnSure.Enabled = false;
this.Literal1.Text = "<script>alert('上传失败!');</script>";
}
else
{
this.btnSure.Enabled = true;
this.Literal1.Text = "<script>alert('上传成功!请点击转入按钮');</script>";
}
}
else
{
this.Literal1.Text = "<script>alert('文件名重复!');</script>";
}
}
else
{
this.Literal1.Text = "<script>alert('请确认你上传你的文件是xls文档!')</script>";
}
}
}
.xls文件转为DataSet的方法
public static System.Data.DataSet ExcelSqlConnection(string filepath)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strCon);
conn.Open();
string strExcel = "select * from [sheet1$]";//excel下方的sheet名稱
OleDbDataAdapter da = new OleDbDataAdapter(strExcel, strCon);
DataSet ds = new DataSet();
try
{
da.Fill(ds);
}
catch (Exception ex)
{
throw new Exception("faile:" + ex.Message);
}
conn.Close();//记得关闭
return ds;
}
读取DataSet
protected void btnSure_Click(object sender, EventArgs e)
{
DataSet ds = ExcelSqlConnection(ViewState["filepath"].ToString());
DataRow[] dr = ds.Tables[0].Select();
for (int i = 0; i < dr.Length; i++)
{
string no = dr[i]["序号"].ToString().Trim(); //序号 为xls 文档首行列名
string id = "{" + System.Guid.NewGuid().ToString().ToUpper() + "}";
string empno = dr[i]["工号"].ToString().Trim();
string name = dr[i]["姓名"].ToString().Trim();
}
//然后再进行数据库操作(省略)
}
导入内容含有日期时处理
因这种方式导入时日期时间字段会变为"2021/9/13 上午 07:00:00"的格式,这种格式无法直接存进数据库,需做特殊处理,附上处理方法。
//日期转换方法
public static string formateDate(string datestr)
{
if(datestr.Substring(5, 2).Contains("/"))// 2021/8/9 || 2021/11/31
{
datestr = datestr.Substring(0, 9).Trim();
}
else
{
datestr = datestr.Substring(0, 10).Trim();
}
return datestr;
}
//时间转换方法
public static string formateTime(string timestr)
{
string date = formateDate(timestr);
string time = timestr.Substring(timestr.Length - 8, 5);
if (timestr.Contains("下午"))
{
time = (int.Parse(time.Substring(0, 2)) + 12) + time.Substring(2, 3);
}
return date + " " + time;
}