我有一个Excel book模板Sheet1,现在要通过程序根据需要把Sheet1复制到Sheet2,sheet3,请教各位用什么方法》》
解决方案 »
- 未处理 System.StackOverflowException类型的异常出现在未知模块中
- html上面的一个标题类的行,如何设置宽度?急,尝试了很多次都失败了!
- Remoting使用时,客户端第一次调用远程对象时,进入远程对象的构造函数?
- 用过webchart的请指点迷经
- webbrowser控件 打开页面成功或失败的判断
- 急救!字符串查找!
- 一个文本文件,aaa.txt,请问,如果读指定的某一行?或者倒序读取?
- 请问在什么情况下两个线程同时侦听同一IP,只有一个线程收的到包,而另一个线程收不到包!
- 欢度国庆,散分200,回来后揭帖
- 如何用C#实现<design patterns>里的singleton(3.5)???
- toolstrip的高度可以增加吗?如何增加?
- 求能在64位Vista上使用的CodeSmith 4.0的注册机,100分相送!
Workbooks("Book1").Worksheets("Sheet2").Past(); 把Book当成数据库, 把Sheet当成数据表.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path";Extended Properties=Excel 8.0;";
OleDbConnection con = new OleDbConnection(sConnectionString);
OleDbCommand cmd=new OleDbCommand("SELECT * FROM ["Sheet1"$]",con);
整体复制的话就这样
Excel.WorkSheet sheet=excelBook.WorkSheets[0];
excelBook.WorkSheets.Add(sheet,Type.Missing,Type.Missing,Type.Missing);
object oMiss = Missing.Value;
Workbook xBook = xApp.Workbooks.Open(templateFilePath, null,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss);
Excel.Worksheet sheet = (Excel.Worksheet)xBook.Worksheets[1];
xBook.Worksheets.Add(sheet,Type.Missing,Type.Missing,Type.Missing);
但是book中只有一个Sheet,根本没有复制出来的
WorkSheets.Add好象只能添加一个空的sheet,不能进行复制
用sheet.Copy(sheet,Type.Missing)可以复制
里面的参数是所复制的sheet的位置
Application xApp = new Application();
object oMiss = Missing.Value;
Workbook xBook = xApp.Workbooks.Open(templateFilePath, null,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss);
Excel.Worksheet sheet = (Excel.Worksheet)xBook.Worksheets[1];
sheet.Copy(sheet,Type.Missing);还是没有复制出来,急啊
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: HRESULT 中的异常:0x800A03EC。源错误:
行 177:
行 178: Excel.Worksheet sheet = (Excel.Worksheet)xBook.Worksheets[1];
行 179: sheet.Copy(sheet,"Sheet2");
行 180:
行 181:
Shett2.Copy(Type.Missing, Shett2);
把Sheet1复制到Sheet2,我当时也为这个问题郁闷了很久!
sheet1怎么没在代码中体现啊
Application xApp = new Application();
object oMiss = Missing.Value;
Workbook xBook = xApp.Workbooks.Open(templateFilePath, null,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss,oMiss);
Excel.Worksheet Shett2= (Excel.Worksheet)xBook.Worksheets.get_Item(2);
Shett2.Copy(Type.Missing, Shett2);
xBook = null;
xApp.Quit();
xApp = null;还是没有复制成功,各位能给出能用的详细代码吗
能不能直接江Sheet2变成新复制的Sheet
Microsoft.Office.Interop.Excel.Workbook _workBook =null;
//头
_workBook = _app.Workbooks.Open(Server.MapPath("PriceStat_01.xls"), Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet _workSheet1 = (Worksheet)_workBook.Worksheets["表尾Excel"];
Microsoft.Office.Interop.Excel.Worksheet _workSheet2= (Worksheet)_workBook.Worksheets["表头Excel"];
Range cell1;
Range cell2;
Range cell3;
Range cell4;
cell1 = (Range)_workSheet1.Cells[1, 1];
cell2 = (Range)_workSheet1.Cells[10,10];
//Sheet1被拷贝的rang
Range rangeBegin = _workSheet1.get_Range(cell1, cell2); cell3 = (Range)_workSheet2.Cells[12,1];
cell4 = (Range)_workSheet2.Cells[22,1]; //Sheet2拷贝到的rang
Range rangeEnd = _workSheet2.get_Range(cell3, cell4);
rangeBegin.Copy(rangeEnd);
//尾部
_workSheet1.Visible = XlSheetVisibility.xlSheetHidden ;
_workBook.SaveAs(Server.MapPath("导出元件1.xls"), 56, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//_workBook2.SaveAs(Server.MapPath("导出元件.xls"), 56, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//垃圾回收
if (_app != null)
{
if (_app.ActiveWorkbook != null)
{
_app.ActiveWorkbook.Close(false, Missing.Value, Missing.Value);
}
_app.Quit();
IntPtr t = new IntPtr(_app.Hwnd);
}
GC.Collect();