因此,我尝试打开一个Excel文件,并使用电子表格中的所有值填充文本框。每当我运行我的代码时,它会从单元格A1中输入值,但这就是它。我不太确定我的循环错误。请提前帮助并表示感谢。
private void LoadButton_Click(object sender, EventArgs e)
{
OpenFileDialog loadFile = new OpenFileDialog();
if (loadFile.ShowDialog() == DialogResult.OK)
{
string rowsAndColumns = "";
fileName = loadFile.FileName;
Excel.Application myApp = new Excel.Application();
myApp.Visible = false;
Excel.Workbook myBook = myApp.Workbooks.Open(fileName);
Excel.Worksheet mySheet = myBook.Sheets[1];
object[,] words = (object[,])mySheet.UsedRange.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);
for (int row = 1; row <= mySheet.UsedRange.Row; row++)
{
for (int column = 1; column <= mySheet.UsedRange.Column; column++)
{
rowsAndColumns += words[row, column];
}
rowsAndColumns += Environment.NewLine;
}
BasicTextBox.Text = rowsAndColumns;
}
}
答案 0 :(得分:0)
尝试以下更改:
for (int row = 1; row <= mySheet.UsedRange.Row.Count; row++)
和
for (int column = 1; column <= mySheet.UsedRange.Column.Count; column++)
和
rowsAndColumns += words[row, column].ToString();