1.操作 打开Excel
protected void operationExcel(object sender, EventArgs e)
{// 服务器存储路径
string path = Server.MapPath("~/temp/");
if (this.seldrFile.HasFile)
{
string filePath = path + this.seldrFile.FileName;
// Excel.exe
Excel.Application excel = new Excel.Application();
// Excel文件 工作薄 对象
Excel._Workbook xBk = null;
// Excel sheet 对象
Excel._Worksheet xSt = null;try
{
object missing = System.Reflection.Missing.Value;
object readOnly = true;
excel.Visible = false;
// 利用FileUpload控件seldrFile 另存到服务器
this.seldrFile.PostedFile.SaveAs(filePath);
// 打开导入文件
excel.Application.Workbooks.Open(filePath, missing, readOnly, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
xBk = excel.Application.Workbooks[1];
// 打开第一个sheet
xSt = (Excel._Worksheet)xBk.Worksheets[1];
}
catch (Exception)
{
// TODO:Exception
}finally
{
if (null != xBk)
{
xBk.Close(false, null, null);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
// 删除临时文件
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
if (file.Exists)
{
file.Delete();
}
GC.Collect();
}
}
}
}