需求分析:之前测试的Excel文件读取,文件是固定放在程序运行的文件夹下,然而实际使用时需要从使用者的电脑上读取Excel文件,这个时间就需要添加选择文件的功能了。
问题分析:选择文件的功能,考虑到在相关使用人的电脑上的Excel文件,想要进行读取,需要先将Excel文件缓存在程序运行的指定文件夹中。下面将逐步记录实现过程中的相关问题及解决方法。
选择文件并获取路径
之前读取Excel文件路径是固定的,文件存储在程序所在的文件夹之下。
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel1.xls") + "; Extended Properties=Excel 8.0;";
要实现选取文件路径,首先需要将读取数据源的路径改为变量名
//strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savePath + "; Extended Properties=Excel 8.0;";//对于Excel 97以上到2003版本
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + savePath + "; Extended Properties=Excel 12.0;";//对于Excel 2007以上到2010版本
注意选择的不同的连接对应不同的Excel版本。具体代码在对之前的处理中做了修改。
public DataSet getExcel(string strpath)
{
string strCon;
//strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savePath + "; Extended Properties=Excel 8.0;";//对于Excel 97以上到2003版本
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + savePath + "; Extended Properties=Excel 12.0;";//对于Excel 2007以上到2010版本
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
myda.Fill(myds);
GridView1.DataSource = myds;
return myds;
}
其他相关代码参阅之前的博客。
另外需要对文件类型进行判断。代码如下:
public bool IsExcel(string str)
{
bool isexcel = false;
string thestr = str.ToLower();
//限定只能上传excel文件
string[] allowExtension = { ".xls", ".xlsx" };
//对上传的文件的类型进行一个个匹对
for (int i = 0; i < allowExtension.Length; i++)
{
if (thestr == allowExtension[i])
{
isexcel= true;
break;
}
}
return isexcel;
}
由于Excel文件是缓存的,实际也是上传,只不过超过指定数量进行清空文件,达到缓存的目的。
private void ClearFile(string FilePath)
{
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
String[] files = System.IO.Directory.GetFiles(FilePath);
if(files.Length > 5)
{
for(int i = 0; i < 5; i++)
{
try
{
System.IO.File.Delete(files[i]);
}
catch
{
}
}
}
}
文件上传部分业务处理代码如下:
public void Button1_Click(object sender, EventArgs e)
{
Boolean fileOk;
if (Excel_upload.HasFile)//验证是否包含文件
{
//取得文件的扩展名,并转换成小写
string fileExtension = Path.GetExtension(Excel_upload.FileName).ToLower();
//验证上传文件是符合格式
fileOk = IsExcel(fileExtension);
if (fileOk)
{
//对上传文件的大小进行检测,限定文件最大不超过8M
if (Excel_upload.PostedFile.ContentLength < 8192000)
{
//string fullPath = Path.GetFullPath(Excel_upload.PostedFile.FileName); //获取文件的绝对路径
//string fileNameNo = Path.GetFileName(pic_upload.PostedFile.FileName); //获取文件名和扩展名
filename = DateTime.Now.ToString("yyyymmddhhMMss") + Excel_upload.FileName;
this.Session["filename"] = filename;
savePath = Server.MapPath(("~/Excel/") + filename);//Server.MapPath 获得虚拟服务器相对路径
//如果已经存在就清空
ClearFile(Server.MapPath("~/Excel/"));
Excel_upload.SaveAs(savePath); //SaveAs将上传的文件内容保存在服务器上
//DataTable dt = createDataSource(savePath);
myds = getExcel(savePath);
GridView1.DataSource = myds;
GridView1.DataBind();
}
else
{
}
}
else
{
Response.Write("<script>alert('Excel文件最大不超过8M!');</script>");
}
}
else
{
Response.Write("<script>alert('请选择Excel文件!');</script>");
}
}
其中,由于局部变量在别的函数中不能够调用,因此考虑使用session存了局部变量文件名称,从而在分页和其他业务处理的时候,便于获取到文件进行读取数据操作。
aspx端需要添加
<td style="text-align: right;" class="auto-style2">
<asp:FileUpload ID="Excel_upload" runat="server" />
</td>
本人小白,欢迎各位大佬指点,一起努力冲冲冲!