c# - 如何使用 Microsoft.Office.Interop 自动从 Excel 中选择数据
问题描述
我在用
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;
并且擅长
当添加超过 7 的数据时,我需要将选择数据设为动态,仅使用 7,因为只有图表选择 7 行,但当 excel 数据自动选择多于或少于 7 行时需要此动态。当添加超过 7 个数据时,excel 中已存在但仅选择 7 行
foreach (Microsoft.Office.Interop.PowerPoint.Shape textShape in slide.Shapes)
{
if (textShape.HasChart == MsoTriState.msoTrue || textShape.HasSmartArt == MsoTriState.msoTrue)
{
ChartData chartData = textShape.Chart.ChartData;
//textShape.Chart.Legend.te = JsonData.twitter_account_analysis.content.content_type[0].name;
chartData.Activate();
Workbook workbook = chartData.Workbook;
Microsoft.Office.Interop.PowerPoint.ChartArea chartArea = textShape.Chart.ChartArea;
workbook.Application.Visible = false;
//workbook.Application.Calculate();
Worksheet dataSheet = workbook.Worksheets[1];
//dataSheet.TableUpdate();
//System.Threading.Thread.Sleep(50);
int firstcolNumber = 2;
int rowNumber = 2;
// Clearing previous data
//dataSheet.UsedRange.Columns[1, Type.Missing].Clear();
//dataSheet.UsedRange.Columns[2, Type.Missing].Clear();
//Dount chart
for (int i = 0; i <99; i++)
{
if (i < JsonData.twitter_metrics.tweets_over_time.Length)
{
dataSheet.Cells[firstcolNumber + i, rowNumber].Clear();
dataSheet.Cells[firstcolNumber + i, 1].Clear();
dataSheet.Cells[firstcolNumber + i, rowNumber] = JsonData.twitter_metrics.tweets_over_time[i].stats_count.ToString();
dataSheet.Cells[firstcolNumber + i, 1] = JsonData.twitter_metrics.tweets_over_time[i].id;
}
else
{
dataSheet.Cells[firstcolNumber + i, rowNumber].Clear();
dataSheet.Cells[firstcolNumber + i, 1].Clear();
}
}
//dataSheet.get_Range("A1", $"B{JsonData.twitter_metrics.tweets_over_time.Length}").Select();
//Marshal.FinalReleaseComObject(dataSheet);
var yAxis = (Microsoft.Office.Interop.PowerPoint.Axis)textShape.Chart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlValue, Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary);
var xAxis = (Microsoft.Office.Interop.PowerPoint.Axis)textShape.Chart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlCategory, Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary);
}
Marshal.ReleaseComObject(textShape);
}
Marshal.ReleaseComObject(slide);
//System.Threading.Thread.Sleep(50);
break;
解决方案
推荐阅读
- javascript - 无法访问 iframe 中 div 的高度
- ios - 到 NSAttributedString 的 HTML 超链接没有样式
- python - 使用 psycopg2 将具有 suqare 括号的字符串写入 postgresql 表时出错
- angular - 可以添加可编辑行的角垫表
- excel - 在多个单元格上进行 Vlookup
- html - base64 嵌入的 PDF 文件不会在 Chrome 中呈现
- python - 为什么我不能在 python 中继承地图?
- mysql - 我想使用 sql 查询显示一个特定的博客有多少喜欢和多少评论
- deep-learning - 数据(图像)规范化的一些最佳实践是什么?
- file - 如何使用 golang 将文件移动到另一个文件夹/路径