我想我已经阅读了这里和 MSDN 上的每一篇文章,但我认为我是一个特例,因为大多数文章和帖子似乎都没有涵盖我想要做的事情。
我正在浏览一个 Excel 表格并提取记录并将它们传递出去,以便它们可以转换为不同的文件类型。我正在尝试使用线程,所以我不会占用 UI,一切都很好,除了更新表单中的进度条。下面是需要更新表单的类:
public class ExcelItem : Form1
{
private string inFile { get; set; }
public ExcelItem(string file)
{
inFile = file;
}
public string getExcelData()
{
string result = "";
int numRec = 0;
int recCount = 0;
Excel.Application xlApplication;
Excel.Workbook xlWorkbook;
Excel.Worksheet xlWorksheet;
Excel.Range xlRange;
xlApplication = new Excel.Application();
xlWorkbook = xlApplication.Workbooks.Open(File, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(2);
xlRange = xlWorksheet.UsedRange;
int rCnt = xlRange.Rows.Count;
int cCnt = xlRange.Columns.Count;
//for (int k = 1; k <= xlWorkbook.Worksheets.Count; k++)
// MessageBox.Show("Found worksheet " + k);
// get the number of records in tne sheet and use numRec to set progress bar max
for (int i = 1; i <= xlRange.Rows.Count; i++)
{
for (int j = 1; j <= xlRange.Columns.Count; j++)
{
if ((((Excel.Range)xlWorksheet.Cells[i, j]).Value2 != null) && (((Excel.Range)xlWorksheet.Cells[i, j]).Value2.ToString() == "Date of Birth"))
{
numRec++;
//code for updating progress bar max would go here
}
}
}
// iterate through records in sheet, use recCount to set progress bar value and return records
for (int i = 1; i <= xlRange.Rows.Count; i++)
{
for (int j = 1; j <= xlRange.Columns.Count; j++)
{
if ((((Excel.Range)xlWorksheet.Cells[i, j]).Value2 != null) && (((Excel.Range)xlWorksheet.Cells[i, j]).Value2.ToString() == "Date of Birth"))
{
result += Environment.NewLine;
recCount++;
// code for updating progress bar value would go here
}
if ((((Excel.Range)xlWorksheet.Cells[i,j]).Value2 != null) && (((Excel.Range)xlWorksheet.Cells[i, j]).Value2.ToString() != ":"))
{
result += (string)((Excel.Range)xlWorksheet.Cells[i, j]).Value2.ToString() + Environment.NewLine;
}
}
}
return result;
}
}
}
返回记录不是问题,只是更新进度条之类的东西现在很头疼。到目前为止,我已经尝试过委托、backgroundworker、BeginInvoker 和线程,但似乎没有任何效果。提前感谢您的帮助。