日期:2014-05-16 浏览次数:21019 次
//复制一份一模一样的Excel文档. 然后对这个新的文件进行更新操作
string UpFileName = DateTime.Now.ToLongTimeString().Replace(":", "") + fileName;
string UpFilePath = FilePath + UpFileName; // 要更新的那个excel文件的全路径
File.Copy(strFilePath, UpFilePath);
Excel.Application app = new Excel.Application();
app.Visible = false;
string DataSource;
OleDbConnection DBConnections;
if (fileName.Substring(fileName.Length - 3) == "xls")
{ // 97 - 03的excel
DataSource = UpFilePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2;\"";
DBConnections = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DataSource);
}
else
{
DataSource = UpFilePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=Yes;IMEX=2;\"";
DBConnections = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + DataSource);
//Response.Write("<script language='javascript'>alert('目前暂不支持excel2007以后的xlsx文件');</script>");
//return;
}
DBConnections.Open();
System.Data.DataTable schemaTable = DBConnections.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
............一些操作逻辑, 和一些不相关的东西, 省略....................
// 更新update Excel表格
SQLString = "update [" + tSheetName + "] set " + dict["测试结果"] + "='" + TCS_ExecResult + "' where " + dict["用例ID"] + "='" + strTestCaseID + "'";
// 语句拼出来是下面这个样子的
// update [客户端操作系统$] set F12='通过' where F11='201902429'
&nb