workApp = new Application();
workbook = workApp.Workbooks.Open(templateFilePath,
missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing,
missing, missing);
workSheet = (Excel.Worksheet)workbook.Sheets[1];
string strSheetName = workSheet.Name.ToString();
for(int i=1;i<=iCount;i++)
{
workbook.Worksheets.Copy(Type.Missing,workSheet);
}
templateFilePath中只有一个sheet,是个模板,我想把这个模板复制两次,即在这个excel中再添加两个
和模板sheet一样的sheet,也就是说iCount=2,可是为什么结果出来,会出现4个sheet呢?
workbook.Worksheets.Copy(Type.Missing,workSheet);这句话如果连续执行三次的话,好象会出现
8个sheet.
12 个解决方案
#1
关注!学习!!
#2
没人来回答?
#3
打开一个workbook时,会有三个sheet的,记得不是很清楚,是不是三个,但肯定不是一个!
你可以先删除掉那些自动产生的,然后你自己添加多少个都可以的!
你可以先删除掉那些自动产生的,然后你自己添加多少个都可以的!
#4
不是那样的,我的模板中只有一个sheet,但是连续执行
workbook.Worksheets.Copy(Type.Missing,workSheet);
就回产生2*2*2这样的效果,为什么?
workbook.Worksheets.Copy(Type.Missing,workSheet);
就回产生2*2*2这样的效果,为什么?
#5
我不知道,学习
#6
怎么会这样,我也用过这个方法,没有问题啊。
除了第二个参数不一样,类似workbook.Worksheets.Copy(Type.Missing,workbook.Sheets[1]);
除了第二个参数不一样,类似workbook.Worksheets.Copy(Type.Missing,workbook.Sheets[1]);
#7
会不会其他地方出错了,这个方法执行了多遍,跟踪过吗,执行了几遍。
#8
占个位学习,并帮你顶一下。
#9
终于看到你错误了,是WorkSheet.Copy,不是WorkBook.Copy,即WorkSheet.Copy(object before,object after);
#10
你可以dataset 导入 excel参考
http://community.csdn.net/Expert/topic/3077/3077526.xml?temp=.8746912
http://www.dev-club.com/club/bbs/showEssence.asp?id=26350
http://dev.csdn.net/Develop/article/18/18623.shtm
http://community.csdn.net/Expert/topic/3112/3112296.xml?temp=.926861
http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50
http://expert.csdn.net/Expert/TopicView1.asp?id=2928057
www.foxhis.com/powermjtest/
原文代码:
private void Button1_Click(object sender, System.EventArgs e)
{
//写入Excel的方法:
//定义需要参数。
string SourceFile="Data.XLS"; //源文件名称。
string TemplatePath=Server.MapPath("ExcelTemplate"); //存放源文件的文件夹路径。
string DownloadPath=Server.MapPath("ExcelDownload"); //副本的文件夹路径。
//副本的文件名。
string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";
object missing = System.Reflection.Missing.Value;
Excel.Application myExcel=new Excel.Application();
//打开新文件
myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
Excel.Workbook myBook=myExcel.Workbooks[1];
Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];
string DownloadFilePath=DownloadPath+"\\"+TempFileName;
int i=0;
while (i<=10)
{
myExcel.Cells[4+i,2]=i.ToString();
myExcel.Cells[4+i,3]=i.ToString();
myExcel.Cells[4+i,4]=i.ToString();
myExcel.Cells[4+i,5]=i.ToString();
myExcel.Cells[4+i,6]=i.ToString();
i++;
}
myBook.Saved=true;
//myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);
myBook.PrintPreview(0);
//myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
myBook.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
myExcel = null;
GC.Collect();
//Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
}
http://community.csdn.net/Expert/topic/3077/3077526.xml?temp=.8746912
http://www.dev-club.com/club/bbs/showEssence.asp?id=26350
http://dev.csdn.net/Develop/article/18/18623.shtm
http://community.csdn.net/Expert/topic/3112/3112296.xml?temp=.926861
http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50
http://expert.csdn.net/Expert/TopicView1.asp?id=2928057
www.foxhis.com/powermjtest/
原文代码:
private void Button1_Click(object sender, System.EventArgs e)
{
//写入Excel的方法:
//定义需要参数。
string SourceFile="Data.XLS"; //源文件名称。
string TemplatePath=Server.MapPath("ExcelTemplate"); //存放源文件的文件夹路径。
string DownloadPath=Server.MapPath("ExcelDownload"); //副本的文件夹路径。
//副本的文件名。
string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";
object missing = System.Reflection.Missing.Value;
Excel.Application myExcel=new Excel.Application();
//打开新文件
myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
Excel.Workbook myBook=myExcel.Workbooks[1];
Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];
string DownloadFilePath=DownloadPath+"\\"+TempFileName;
int i=0;
while (i<=10)
{
myExcel.Cells[4+i,2]=i.ToString();
myExcel.Cells[4+i,3]=i.ToString();
myExcel.Cells[4+i,4]=i.ToString();
myExcel.Cells[4+i,5]=i.ToString();
myExcel.Cells[4+i,6]=i.ToString();
i++;
}
myBook.Saved=true;
//myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);
myBook.PrintPreview(0);
//myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
myBook.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
myExcel = null;
GC.Collect();
//Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
}
#11
回复人: tfrtfr(tfrtfr) ( ) 信誉:100 2004-11-12 13:03:00 得分: 0
终于看到你错误了,是WorkSheet.Copy,不是WorkBook.Copy,即WorkSheet.Copy(object before,object after);
你说的WorkBook.Copy,我哪里用了WorkBook.Copy了?
终于看到你错误了,是WorkSheet.Copy,不是WorkBook.Copy,即WorkSheet.Copy(object before,object after);
你说的WorkBook.Copy,我哪里用了WorkBook.Copy了?
#12
谢谢各位了,特别是tfrtfr(tfrtfr) ( )
#1
关注!学习!!
#2
没人来回答?
#3
打开一个workbook时,会有三个sheet的,记得不是很清楚,是不是三个,但肯定不是一个!
你可以先删除掉那些自动产生的,然后你自己添加多少个都可以的!
你可以先删除掉那些自动产生的,然后你自己添加多少个都可以的!
#4
不是那样的,我的模板中只有一个sheet,但是连续执行
workbook.Worksheets.Copy(Type.Missing,workSheet);
就回产生2*2*2这样的效果,为什么?
workbook.Worksheets.Copy(Type.Missing,workSheet);
就回产生2*2*2这样的效果,为什么?
#5
我不知道,学习
#6
怎么会这样,我也用过这个方法,没有问题啊。
除了第二个参数不一样,类似workbook.Worksheets.Copy(Type.Missing,workbook.Sheets[1]);
除了第二个参数不一样,类似workbook.Worksheets.Copy(Type.Missing,workbook.Sheets[1]);
#7
会不会其他地方出错了,这个方法执行了多遍,跟踪过吗,执行了几遍。
#8
占个位学习,并帮你顶一下。
#9
终于看到你错误了,是WorkSheet.Copy,不是WorkBook.Copy,即WorkSheet.Copy(object before,object after);
#10
你可以dataset 导入 excel参考
http://community.csdn.net/Expert/topic/3077/3077526.xml?temp=.8746912
http://www.dev-club.com/club/bbs/showEssence.asp?id=26350
http://dev.csdn.net/Develop/article/18/18623.shtm
http://community.csdn.net/Expert/topic/3112/3112296.xml?temp=.926861
http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50
http://expert.csdn.net/Expert/TopicView1.asp?id=2928057
www.foxhis.com/powermjtest/
原文代码:
private void Button1_Click(object sender, System.EventArgs e)
{
//写入Excel的方法:
//定义需要参数。
string SourceFile="Data.XLS"; //源文件名称。
string TemplatePath=Server.MapPath("ExcelTemplate"); //存放源文件的文件夹路径。
string DownloadPath=Server.MapPath("ExcelDownload"); //副本的文件夹路径。
//副本的文件名。
string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";
object missing = System.Reflection.Missing.Value;
Excel.Application myExcel=new Excel.Application();
//打开新文件
myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
Excel.Workbook myBook=myExcel.Workbooks[1];
Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];
string DownloadFilePath=DownloadPath+"\\"+TempFileName;
int i=0;
while (i<=10)
{
myExcel.Cells[4+i,2]=i.ToString();
myExcel.Cells[4+i,3]=i.ToString();
myExcel.Cells[4+i,4]=i.ToString();
myExcel.Cells[4+i,5]=i.ToString();
myExcel.Cells[4+i,6]=i.ToString();
i++;
}
myBook.Saved=true;
//myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);
myBook.PrintPreview(0);
//myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
myBook.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
myExcel = null;
GC.Collect();
//Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
}
http://community.csdn.net/Expert/topic/3077/3077526.xml?temp=.8746912
http://www.dev-club.com/club/bbs/showEssence.asp?id=26350
http://dev.csdn.net/Develop/article/18/18623.shtm
http://community.csdn.net/Expert/topic/3112/3112296.xml?temp=.926861
http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50
http://expert.csdn.net/Expert/TopicView1.asp?id=2928057
www.foxhis.com/powermjtest/
原文代码:
private void Button1_Click(object sender, System.EventArgs e)
{
//写入Excel的方法:
//定义需要参数。
string SourceFile="Data.XLS"; //源文件名称。
string TemplatePath=Server.MapPath("ExcelTemplate"); //存放源文件的文件夹路径。
string DownloadPath=Server.MapPath("ExcelDownload"); //副本的文件夹路径。
//副本的文件名。
string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";
object missing = System.Reflection.Missing.Value;
Excel.Application myExcel=new Excel.Application();
//打开新文件
myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
Excel.Workbook myBook=myExcel.Workbooks[1];
Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];
string DownloadFilePath=DownloadPath+"\\"+TempFileName;
int i=0;
while (i<=10)
{
myExcel.Cells[4+i,2]=i.ToString();
myExcel.Cells[4+i,3]=i.ToString();
myExcel.Cells[4+i,4]=i.ToString();
myExcel.Cells[4+i,5]=i.ToString();
myExcel.Cells[4+i,6]=i.ToString();
i++;
}
myBook.Saved=true;
//myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);
myBook.PrintPreview(0);
//myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
myBook.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
myExcel = null;
GC.Collect();
//Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
}
#11
回复人: tfrtfr(tfrtfr) ( ) 信誉:100 2004-11-12 13:03:00 得分: 0
终于看到你错误了,是WorkSheet.Copy,不是WorkBook.Copy,即WorkSheet.Copy(object before,object after);
你说的WorkBook.Copy,我哪里用了WorkBook.Copy了?
终于看到你错误了,是WorkSheet.Copy,不是WorkBook.Copy,即WorkSheet.Copy(object before,object after);
你说的WorkBook.Copy,我哪里用了WorkBook.Copy了?
#12
谢谢各位了,特别是tfrtfr(tfrtfr) ( )