vsto实现excel指定非连续列一屏展示,用户不通过拖动横向滚动查看隐藏的区域
一屏展示的列中列数据很对要根据屏幕的宽度比列调整各列的宽度
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excelApp = Globals.ThisAddIn.Application;
Excel.Worksheet activeWorksheet = excelApp.ActiveSheet;
// 获取当前窗口的宽度
int windowWidth = excelApp.ActiveWindow.Width;
// 获取一屏展示的所有列的总宽度
Excel.Range visibleRange = excelApp.ActiveWindow.VisibleRange;
double totalWidth = 0;
foreach (Excel.Range column in visibleRange.Columns)
{
totalWidth += column.Width;
}
// 计算每列的新宽度
for (int i = 1; i <= visibleRange.Columns.Count; i++)
{
Excel.Range column = visibleRange.Columns[i];
double originalWidth = column.Width;
double newWidth = (originalWidth / totalWidth) * windowWidth;
column.ColumnWidth = newWidth;
}
根据比列乘以列宽,列没有铺满,小数精度导致
但是Range.ColumnWidth属性double类型只保留2位小数,永远不准