After some testing, it appears there are few issues.
This code is now copying the file.. After the File.Copy operation, there is code following that should update certain cells, when attempting to update, it systems falls over with an error advising it cant find that specific cell A28 for example.
When reverting my code back to simply overwriting the original, it finds the cell – A28 and updates the value with no issues.
Any ideas?
Code as is stands (with overwriting original Template):
// Declaration of variables
ClientName = txtClientName.Text;
string newFileName = ClientName + ".xls";
string Filename = "C:\\Template.xls";
//File.Copy(Filename, @"C:\\" + newFileName, true);
// If you are using xls format (2003), use this connection string.
string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filename + ";Extended Properties=\"Excel 8.0;HDR=NO;\"";
string SQL1 = "UPDATE [Cover Sheet$A28:A28] SET F1='" + ClientName + "'";
using (OleDbConnection Connection = new OleDbConnection(ConnectionString))
{
Connection.Open();
using (OleDbCommand cmd1 = new OleDbCommand(SQL1, Connection))
{
cmd1.ExecuteNonQuery();
}
}
}
Pass to your procedure the name of the new file name desired, copy the template and work on the new file.
This change (Calling File.Copy) requires the adding of this line to the beginning of the source code file:
This simple line inform the compiler that you are using objects, classes, enums and interfaces present in the System.IO namespace. Without the line you should prefix every class like File with its fully qualified namespace
not very typing convenient as you can see.
EDIT: Following the comments below I will add another tip.
If you pass in a simple file name without path, the File.Copy creates the new file in the current working directory. The current working directory When you are working within Visual Studio is
bin/debugorbim/release. When you deploy your application, the current working directory will be the directory where your application starts.You have some options:
“C:\MyFiles\destinationFile.xls”).
filename passed in.
directory relative to a well known paths (like MyDocuments)