C# 在c中为Excel生成名称#
我在excel中有一个模板,我用它从应用程序中生成报告 每次生成报告时,它都会保存在一个文件夹中,该文件夹具有固定名称和变量,因此它并不总是同一个文档 我有这个代码来打开模板C# 在c中为Excel生成名称#,c#,excel,C#,Excel,我在excel中有一个模板,我用它从应用程序中生成报告 每次生成报告时,它都会保存在一个文件夹中,该文件夹具有固定名称和变量,因此它并不总是同一个文档 我有这个代码来打开模板 Excel.Application app = new Excel.Application(); Excel._Workbook book; Excel._Worksheet sheet; libro = app.Workbooks.Open(@"path", Type.Missing, Type.
Excel.Application app = new Excel.Application();
Excel._Workbook book;
Excel._Worksheet sheet;
libro = app.Workbooks.Open(@"path", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
使用此选项,我保存并生成0中带有计数器的名称
string paths= @"path";
string name= "Report_.xlsx";
while (File.Exists(System.IO.Path.Combine(paths, name)))
{
int i = 1;
name= string.Format("Report_{0}.xlsx",i);
}
string fullpath = Path.Combine(paths, name);
book.SaveAs(fullpath, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value);
book.Close(0);
app.Quit();
问题是,我只能生成两个报告,但随后我尝试生成第三方,应用程序被卡住了,我没有得到任何异常,它只会被卡住,但只有在我尝试生成第三个文档时才会将计数器变量初始化移到循环之外
string paths= @"path";
string name= "Report_.xlsx";
int i = 1;
while (File.Exists(System.IO.Path.Combine(paths, name)))
{
name= string.Format("Report_{0}.xlsx",i);
i++;
}
string fullpath = Path.Combine(paths, name);
book.SaveAs(fullpath, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value);
book.Close(0);
app.Quit();
将计数器变量初始化移到循环之外
string paths= @"path";
string name= "Report_.xlsx";
int i = 1;
while (File.Exists(System.IO.Path.Combine(paths, name)))
{
name= string.Format("Report_{0}.xlsx",i);
i++;
}
string fullpath = Path.Combine(paths, name);
book.SaveAs(fullpath, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value);
book.Close(0);
app.Quit();
你不是在增加变量“i”。您应该在while循环外部声明它,然后在使用前增加:
string paths= @"path";
string name= "Report_.xlsx";
int i = 0;
while (File.Exists(System.IO.Path.Combine(paths, name)))
{
i++;
name= string.Format("Report_{0}.xlsx",i);
}
string fullpath = Path.Combine(paths, name);
book.SaveAs(fullpath, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value);
book.Close(0);
app.Quit();
你不是在增加变量“i”。您应该在while循环外部声明它,然后在使用前增加:
string paths= @"path";
string name= "Report_.xlsx";
int i = 0;
while (File.Exists(System.IO.Path.Combine(paths, name)))
{
i++;
name= string.Format("Report_{0}.xlsx",i);
}
string fullpath = Path.Combine(paths, name);
book.SaveAs(fullpath, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value);
book.Close(0);
app.Quit();