string constr="ODBC;Driver=SQL Server;Server=goan;database=UD_Manufacture;uid=acmesql;pwd=usig";
string sqlstr="select top 10 PMat,PAc from basedata";Excel.Application myexcel=new Excel.ApplicationClass();
myexcel.Visible=true;
Excel._Workbook xBk=myexcel.Workbooks.Add(Type.Missing);
Excel._Worksheet xSt=xSt=(Excel._Worksheet)xBk.ActiveSheet;
Excel._QueryTable xQt=xSt.QueryTables.Add(constr,xSt.get_Range(myexcel.Cells[4,2],myexcel.Cells[4,2]),sqlstr);
xQt.Refresh(xQt.BackgroundQuery);
Excel._Chart xCt=(Excel._Chart)xBk.Charts.Add(Type.Missing,Type.Missing,1,Type.Missing);
xBk.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject,"Sheet1");
myexcel.ActiveChart.SetSourceData(myexcel.get_Range(myexcel.Cells[4,2],myexcel.Cells[8,3]),Excel.XlRowCol.xlColumns);错误提示:
未处理的“System.Runtime.InteropServices.COMException”类型的异常出现在 ExcelTest.exe 中。其他信息: HRESULT 中的异常:0x800A03EC。
string sqlstr="select top 10 PMat,PAc from basedata";Excel.Application myexcel=new Excel.ApplicationClass();
myexcel.Visible=true;
Excel._Workbook xBk=myexcel.Workbooks.Add(Type.Missing);
Excel._Worksheet xSt=xSt=(Excel._Worksheet)xBk.ActiveSheet;
Excel._QueryTable xQt=xSt.QueryTables.Add(constr,xSt.get_Range(myexcel.Cells[4,2],myexcel.Cells[4,2]),sqlstr);
xQt.Refresh(xQt.BackgroundQuery);
Excel._Chart xCt=(Excel._Chart)xBk.Charts.Add(Type.Missing,Type.Missing,1,Type.Missing);
xBk.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject,"Sheet1");
myexcel.ActiveChart.SetSourceData(myexcel.get_Range(myexcel.Cells[4,2],myexcel.Cells[8,3]),Excel.XlRowCol.xlColumns);错误提示:
未处理的“System.Runtime.InteropServices.COMException”类型的异常出现在 ExcelTest.exe 中。其他信息: HRESULT 中的异常:0x800A03EC。
----------------------------------------------------------------------- webserv2 beijing
Public Sub InsertActiveChart(ByVal ChartType As Excel.XlChartType, ByVal ws As String, ByVal DataSourcesX1 As Integer, ByVal DataSourcesY1 As Integer, ByVal DataSourcesX2 As Integer, ByVal DataSourcesY2 As Integer, Optional ByVal ChartDataType As Excel.XlRowCol = Excel.XlRowCol.xlColumns)
wb.Charts.Add()
With wb.ActiveChart
.ChartType = ChartType
.SetSourceData(GetSheet(ws).Range(GetSheet(ws).Cells(DataSourcesX1, DataSourcesY1), GetSheet(ws).Cells(DataSourcesX2, DataSourcesY2)), ChartDataType)
.Location(Excel.XlChartLocation.xlLocationAsObject, ws)
End With
End Sub#End Region
我代码中只有myexcel.ActiveChart.SetSourceData(myexcel.get_Range(myexcel.Cells[4,2],myexcel.Cells[8,3]),Excel.XlRowCol.xlColumns);这一句才会出错。
请参阅此消息的结尾。************** 异常文本 **************
System.Runtime.InteropServices.COMException (0x800A03EC): HRESULT 中的异常:0x800A03EC。
at Excel.ApplicationClass.get_Cells()
at ExcelTest.Form1.ExportExcel()
at ExcelTest.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** 已加载的程序集 **************
mscorlib
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
ExcelTest
程序集版本: 1.0.2053.19864
Win32 版本: 1.0.2053.19864
基本代码: file:///F:/csharp/ExcelText/ExcelTest/bin/Release/ExcelTest.exe
----------------------------------------
System.Windows.Forms
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
Interop.Excel
程序集版本: 1.5.0.0
Win32 版本: 1.5.0.0
基本代码: file:///F:/csharp/ExcelText/ExcelTest/bin/Release/Interop.Excel.DLL
----------------------------------------
mscorlib.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/mscorlib.resources/1.0.5000.0_zh-chs_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
程序集版本: 1.0.5000.0
Win32 版本: 1.1.4322.573
基本代码: file:///c:/windows/assembly/gac/system.windows.forms.resources/1.0.5000.0_zh-chs_b77a5c561934e089/system.windows.forms.resources.dll
----------------------------------------************** JIT 调试 **************
计算机的配置文件(machine.config)的
system.windows.forms 节中必须设置 jitDebugging 值。
编译应用程序时还必须启用\r\n调试。\r\n\r\n例如: \r\n\r\n<configuration>\r\n <system.windows.forms jitDebugging="true" />\r\n</configuration>\r\n\r\n启用 JIT 调试后,任何未处理的异常\r\n都将被发送到此计算机上注册的 JIT 调试器,\r\n而不是由此对话框处理。\r\n
exc.ActiveChart.ChartType=Excel.XlChartType.xlColumnClustered;
exc.ActiveChart.SetSourceData(worksheet.get_Range("A1","B10"),Excel.XlRowCol.xlColumns);
exc.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject,"sheet1");
exc.ActiveChart.HasTitle=true;
exc.ActiveChart.ChartTitle.Text="图表名称";
exc.ActiveChart.HasDataTable=false;
to anbeel(番薯好吃,但不宜多吃) 两位高手,你们两个的都可以,谢谢!对了,透视表怎样加呢?
pcPivotCache = xlApp.ActiveWorkbook.PivotCaches().Add(Excel.
XlPivotTableSourceType.xlExternal,
Type.Missing);rngRange = activeSheet.get_Range("A5", Type.Missing);
pcPivotCache.CreatePivotTable(rngRange, "MyPivotTable", Type.Missing,
Type.Missing);
object oM=Type.Missing;
try
{
string constr="Provider=SQLOLEDB;Data Source=goan;Initial Catalog=UD_Manufacture;User Id=xx;Password=yy;";
string sqlstr="select top 3 PMat,PSty,PAc from basedata"; ADODB.ConnectionClass con=new ADODB.ConnectionClass();
con.Open(constr,"","",0);
ADODB.RecordsetClass rst=new ADODB.RecordsetClass();
rst.Open(sqlstr,con,ADODB.CursorTypeEnum.adOpenStatic,ADODB.LockTypeEnum.adLockReadOnly,0); Excel.Application myexcel=new Excel.ApplicationClass();
myexcel.Visible=true;
Excel._Workbook xBk=myexcel.Workbooks.Add(oM);
Excel._Worksheet xSt=xSt=(Excel._Worksheet)xBk.ActiveSheet; Excel.PivotCache xPc;
Excel.Range xRg;
xRg=xSt.get_Range("B4",oM); xPc=myexcel.ActiveWorkbook.PivotCaches().Add(Excel.XlPivotTableSourceType.xlExternal,oM);//这里出错
xPc.Recordset=rst;
//xPc.CreatePivotTable(xRg,"xx",true);
}
谢谢!//以上是取得数据集rstRecordset的代码,略Set xPc=ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)//这里转成C#代码后总出错
Set xPc.Recordset=rstRecordsetxPc=myexcel.ActiveWorkbook.PivotCaches().Add(Excel.XlPivotTableSourceType.xlExternal,Type.Missing);
错在哪呢?