- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想在一个工作表中创建多个数据透视表,并从每个数据透视表创建一个图表。第一个数据透视表和第一个图表是正常创建的。创建第二个数据透视表后,我想添加第二个图表,其源来自第二个数据透视表,但这比 HRESULT E_FAIL 重新调整,第二个图表具有第一个数据透视表的源,我无法更改。
string pitpivotNam1 = "table1";
Dictionary<String, Excel.PivotField> pFields = new Dictionary<String, Excel.PivotField>();
Excel.PivotTable pivotTable1 = null;
Excel.Range pivotDest = excelApp.Worksheets["PivotTable"].Cells[1, 1];
excelApp.Worksheets[sheetData].Activate();
int countColmn = excelApp.Cells[1, 1].End(Excel.XlDirection.xlToRight).Column;
var headerNam = excelApp.Range[excelApp.Cells[1, 1], excelApp.Cells[1, countColmn]].Value;
// Data Source for PivotTable
Excel.Range pivotData = excelApp.Worksheets[sheetData].usedRange;
Excel.PivotCache pivotcache = excelApp.ActiveWorkbook.PivotCaches().Create(Excel.XlPivotTableSourceType.xlDatabase, pivotData.Address/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
excelApp.Worksheets[sheetDest].Activate();
pivotTable1 = excelApp.Worksheets[sheetDest].PivotTables.Add(pivotcache, pivotDest, pitpivotNam1, useDefault/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
// Create Dynamically PivotFields
for (int i = 1; i <= headerNam.Length - 1; i++)
{
pFields.Add("test" + i.ToString(), (Excel.PivotField)pivotTable1.PivotFields(i));
}
pFields["test20"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields["test20"].Function = Excel.XlConsolidationFunction.xlAverage;
pFields["test43"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields["test43"].Function = Excel.XlConsolidationFunction.xlAverage;
pFields["test66"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields["test66"].Function = Excel.XlConsolidationFunction.xlAverage;
pFields["test20"].Name = "DVP & R Progress";
// First Chart
excelApp.ActiveSheet.Shapes.AddChart.Select();
excelApp.ActiveChart.SetSourceData(pivotTable1.TableRange1);
// Second PivotTable
string pitpivotNam2 = "table2";
Excel.PivotTable pivotTable2 = null;
Dictionary<String, Excel.PivotField> pFields2 = new Dictionary<String, Excel.PivotField>();
Excel.Range pivotDest2 = excelApp.Worksheets["PivotTable"].Cells[20, 1];
excelApp.Worksheets[sheetData].Activate();
int countColmn2 = excelApp.Cells[1, 1].End(Excel.XlDirection.xlToRight).Column;
var headerNam2 = excelApp.Range[excelApp.Cells[1, 1], excelApp.Cells[1, countColmn2]].Value;
// Data Source for PivotTable
Excel.Range pivotData2 = excelApp.Worksheets[sheetData].usedRange;
Excel.PivotCache pivotcache2 = excelApp.ActiveWorkbook.PivotCaches().Create(Excel.XlPivotTableSourceType.xlDatabase, pivotData2.Address/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
excelApp.Worksheets[sheetDest].Activate();
pivotTable2 = excelApp.Worksheets[sheetDest].PivotTables.Add(pivotcache2, pivotDest2, pitpivotNam2, useDefault/*, Excel.XlPivotTableVersionList.xlPivotTableVersion15*/);
// Create Dynamically PivotFields
for (int i = 1; i <= headerNam2.Length - 1; i++)
{
pFields2.Add("test" + i.ToString(), (Excel.PivotField)pivotTable2.PivotFields(i));
}
pFields2["test17"].Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
Excel.PivotField dataField = pivotTable2.DataPivotField;
pFields2["test17"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields2["test17"].Function = Excel.XlConsolidationFunction.xlCount;
pFields2["test40"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields2["test40"].Function = Excel.XlConsolidationFunction.xlCount;
pFields2["test63"].Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pFields2["test63"].Function = Excel.XlConsolidationFunction.xlCount;
dataField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
// Secound Chart
excelApp.ActiveSheet.Shapes.AddChart.Select();
excelApp.ActiveChart.SetSourceData(pivotTable2.TableRange1);
提前感谢您的帮助。
问候,
丹尼尔
最佳答案
我在 VSTO C# 项目中遇到了类似的问题。
在我的例子中,我必须做的是在创建图表之前选择枢轴范围。
我发现,如果在创建图表时选择了第一个数据透视表的单元格,则图表将绑定(bind)到该数据透视表并且您无法更改源数据(事实上,您甚至不能这样做在 Excel 中,图表的源区域是灰色的)。
我通过在创建图表之前选择数据透视单元格解决了这个问题。
Range rangePivot2 = pivotTable2.TableRange1;
rangePivot2.Select();
Chart chart = sheet.Controls.AddChart(cellsWhereTheChartWillBeDrawn, "secondChart");
chart.SetSourceData(rangePivot2); /* this won't give you E_FAIL... but it's not really needed */
我的代码示例是 VSTO 伪代码,但我相信类似的方法也适用于纯 Excel COM 自动化。
关于c# - 图表的 SetSourceData 返回数据透视表的 HRESULT E_FAIL (Excel C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39741268/
ID3D11Device* md3dDevice; ID3D11DeviceContext* md3dImmediateContext; D3D_DRIVER_TYPE md3dDriverType
当我在MFC应用程序中使用ATL时(不要问我为什么,只是因为我喜欢),有时我得到错误代码:E_FAIL。但是,我要定位具体原因几乎没有用。 我在谷歌上搜索了很多次,但没有找到任何相关内容。我认为 AT
我有一个小型应用程序,可以加载模型并在没有灯光或其他任何东西的情况下显示它。我一直在尝试构建一个版本并在另一台计算机上运行它,但 D3DX10CreateEffectFromFile 一直返回 E_F
我有一个图像检测模块,它被封装为一个 COM 模块。我导出了一个 Key/Value Getter API,例如:GetImageAttr(UINT key, void* pValue);。我们的产品
我使用 Kepware KEPServerEX 5.15 作为我的 OPC 服务器和 .NET 中的 OPC DA Automation Wrapper 2.02 进行通信。 当我的应用程序和 KEP
我在 Excel 中有一个非常简单的 VBA 代码,它使用 ADO 查询 MariaDB 数据库,然后将该记录集复制到 Excel 中。问题是当代码在某些机器上运行时,它运行良好,而在其他机器上它返回
请检查以下代码,它应该连接到当前正在运行的 Excel: #include #include #import "C:\Program Files (x86)\Common Files\micros
我有一个 Ubuntu 服务器,只花了 2 天时间将一个 22GB 的多部分存档传输到它。 22GB 被分成 1800 个 10MB 的部分。 每当我尝试提取它们时(p7zip 9.20): 7z x
在 VS-2017 中,单击“添加引用”时出现错误: "Error HRESULT E_FAIL has been returned from a call to a COM component."
我在自己的窗口中托管 Web 浏览器控件。以下是相关步骤: CoGetClassObject(CLSID_WebBrowser, CLSCTX_INPROC_SERV
MSDN(https://msdn.microsoft.com/en-us/library/04tsf4b5.aspx)说: Return Value Returns S_OK on success,
我刚开始阅读这本书并尝试实现一些 CSS 代码。每当我尝试创建一个新的 CSS 文件或打开一个 css 文件时,我都会收到以下错误:调用 COM 组件返回错误 HRESULT E_FAIL。 谁能告诉
我有一个 C++ 调用 C# 表单。它似乎工作正常。但是,当我打开 C# 项目窗体设计器时,会弹出以下错误消息。有什么问题。解决方案构建并运行良好。 调用 COM 组件返回错误 HRESULT E_F
我正在使用以下代码调用 D3D11CreateDevice(): ID3D11Device* md3dDevice; ID3D11DeviceContext* md3dImmediateContext
在 Silverlight 4 应用程序中;这个错误是什么意思?: "Error HRESULT E_FAIL has been returned from a call to a COM compo
你好, 我在使用一些简单的 VBA 时遇到了问题。我编写了一个脚本来进行文本查询(没有输入参数,因此没有真正的用户交互、SQL 注入(inject)等),针对数据库运行它,并将其转储到新的工作表中。这
我对Docker有问题。 我打开然后关闭了Hyper-v,这无济于事。 Hyper-v技术-开启(在BIOS中) I have this error 最佳答案 那对我有帮助: ''' 嘿伙计们,我也遇
我们在 Office 2007 中的 .NET 3.5 中开发了一个复杂的应用程序。在某些表单中,我们使用 WebBrowser 控件导航到我们的 HTML 页面。问题是在某些机器上,当控件调用“导航
我不知道这个“错误 HRESULT E_FAIL 已从对 COM 组件的调用返回”是怎么回事。各位知道怎么回事吗?我的 wcf 服务运行良好,但今天出现此错误。我在谷歌上搜索,但没有找到答案。 提前致
我的应用程序中有一些 UserControl,我需要支持从中拖放,因此我将代码提取到扩展 UserControl 的抽象类中(下面的代码)。当我在一个控件中使用它时,它是 ListBox 中 Data
我是一名优秀的程序员,十分优秀!