Vsto Excel的一个困惑的问题 [英] A puzzled problem with Vsto Excel

查看:76
本文介绍了Vsto Excel的一个困惑的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
使用vsto,以下代码非常令人困惑,以至于我找不到问题.
当我将它们放入[private void Sheet1_Startup(object sender,System.EventArgs e)]时,运行没有错误,
但是如果我将它们放入[private void button1_Click(object sender,EventArgs e)],则运行时错误.
谢谢!

Hi All,
Used vsto, the below codes are so puzzled that i couldn''t find the problem.
When i put them into [private void Sheet1_Startup(object sender, System.EventArgs e)], run with no error,
but if i put them into [private void button1_Click(object sender, EventArgs e)], run with a error of runtime.
Thanks!

Sheet1 st = Globals.Sheet1;

List<int> tagList = new List<int>();
Excel.Range rng;
string rngValue;
int d = st.UsedRange.EntireRow.Count;
for (int i = 12; i <= st.UsedRange.EntireRow.Count + 1; i++)
{
    rng = st.get_Range(Cells[i, 4], Cells[i, 4]);
    rngValue = rng.Value2 as string;
    if (string.IsNullOrEmpty(rngValue))
    {
        tagList.Add(i);
    }
}

st.Application.ScreenUpdating = false;
st.Application.DisplayAlerts = false;

//Set Font type and size
rng = st.get_Range(st.Cells[12, 1],
        st.Cells[st.UsedRange.EntireRow.Count, st.UsedRange.EntireColumn.Count]);
rng.Font.Size = 10;
rng.Font.Name = "Calibri";
//background color - white
rng.Interior.ColorIndex = 2;

for (int i = 0; i < tagList.Count - 1; i++)
{
    rng = st.get_Range(Cells[tagList[i], 3], Cells[tagList[i], 3]);
    rng.Value2 = string.Empty;

    //Merge
    rng = st.get_Range(Cells[tagList[i] + 1, 2], Cells[tagList[i + 1] - 1, 2]);
    rng.Merge(System.Reflection.Missing.Value);
    rng.Value2 = string.Empty;

    rng = st.get_Range(Cells[tagList[i] + 1, 3], Cells[tagList[i + 1] - 1, 3]);
    string rngValueTemp = rng.FormulaR1C1 as string;
    rng.Merge(System.Reflection.Missing.Value);
    rng.Value2 = rngValueTemp;
    rng.Font.Underline = Excel.XlUnderlineStyle.xlUnderlineStyleSingle;
    rng.HorizontalAlignment = Excel.Constants.xlLeft;
    rng.VerticalAlignment = Excel.Constants.xlTop;

    //add backcolor and set size
    rng = st.get_Range(Cells[tagList[i], 1], Cells[tagList[i], st.UsedRange.EntireColumn.Count]);
    rng.Interior.ColorIndex = Excel.Constants.xlAutomatic;
    rng.Interior.Color = 16751103;
    rng.Font.Bold = true;

    //set border
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.Constants.xlNone;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.Constants.xlNone;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeTop).Weight = Excel.XlBorderWeight.xlMedium;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlContinuous;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlMedium;
}

//set border (DataBlock)
for (int j = 0; j < 6; j++)
{
    rng = st.get_Range(st.Cells[12, 5 + j * 7],
        st.Cells[st.UsedRange.EntireRow.Count, 5 + (j + 1) * 7 - 1]);
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlContinuous;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeLeft).Weight = Excel.XlBorderWeight.xlMedium;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlContinuous;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeRight).Weight = Excel.XlBorderWeight.xlMedium;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeTop).Weight = Excel.XlBorderWeight.xlMedium;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlContinuous;
    rng.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlMedium;
}

st.Application.ScreenUpdating = true;

推荐答案

这是不确定性的问题.当将这些代码放入Click Method按钮时,控制台将收到错误消息-[来自HRESULT的异常:0x800A03EC],但是如果放入Sheet_StartUp(),则这些代码可以正常运行,问题:我想知道为什么? 谢谢
it''s a problem of nondeterminacy. When these codes put into button Click Method , the console will get a error message -[Exception from HRESULT: 0x800A03EC],but if put into Sheet_StartUp(), these could run with no error, Question: I want to know why? Thanks


这篇关于Vsto Excel的一个困惑的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆