简体   繁体   English

C#将Excel另存为Copy

[英]C# to Save Excel AsCopy

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. 现在,此代码正在复制文件。在File.Copy操作之后,下面的代码应更新某些单元格,当尝试更新时,它的系统File.Copy并提示错误,例如,它找不到特定的单元格A28。

When reverting my code back to simply overwriting the original, it finds the cell - A28 and updates the value with no issues. 将我的代码恢复为仅覆盖原始代码时,它将找到单元格A28并毫无问题地更新值。

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. 将所需新文件名的名称传递给您的过程,然后复制模板并处理新文件。

public void MapToExcelDocument(string newFileName) 
{ 
    // Declaration of variables  
    string templateName = "C:\\Template.xls"; 
    File.Copy(templateName, newFileName, true);

    // If you are using xls format (2003), use this connection string.             
    string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
                               newFileName + ";Extended Properties=\"Excel 8.0;HDR=NO;\""; 

 ....

This change (Calling File.Copy) requires the adding of this line to the beginning of the source code file: 此更改(调用File.Copy)要求在源代码文件的开头添加以下行:

using System.IO;

This simple line inform the compiler that you are using objects, classes, enums and interfaces present in the System.IO namespace. 这行简单的代码告诉编译器您正在使用System.IO名称空间中存在的对象,类,枚举和接口。 Without the line you should prefix every class like File with its fully qualified namespace 如果没有该行,则应在每个类(如File)前加上其完全限定的名称空间

System.IO.FileCopy(templateName, newFileName, true);

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. 如果传入一个不带路径的简单文件名,则File.Copy将在当前工作目录中创建新文件。 The current working directory When you are working within Visual Studio is bin/debug or bim/release . 在Visual Studio中工作时,当前工作目录为bin/debugbim/release When you deploy your application, the current working directory will be the directory where your application starts. 部署应用程序时,当前工作目录将是应用程序启动的目录。

You have some options: 您有一些选择:

  • Pass a fully qualified filename to an arbitrary directory (IE "C:\\MyFiles\\destinationFile.xls"). 将完全限定的文件名传递到任意目录(即“ C:\\ MyFiles \\ destinationFile.xls”)。
  • Define a path inside your config file and add this path to the filename passed in. 在配置文件中定义一个路径,并将该路径添加到传入的文件名中。
  • Define a relative path in your config file and write in a directory relative to a well known paths (like MyDocuments) 在配置文件中定义一个相对路径,并相对于一个众所周知的路径(如MyDocuments)写入一个目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM