c#程序 代码如下,打开一个excel文件,指定写入位置,但是程序运行后不能将数据写入excel,请问为什么?
Excel.Applicationapp;Excel.Workbookswbs;Excel.Workbookwb;Excel.Worksheetworksheet;Exc...
Excel.Application app;
Excel.Workbooks wbs;
Excel.Workbook wb;
Excel.Worksheet worksheet;
Excel.Range range;
try
{
string excelName = "";
OpenFileDialog dlg = new OpenFileDialog();
dlg.ShowDialog();
excelName = dlg.FileName;
app = new Excel.Application();
wbs = app.Workbooks;
wb = wbs.Add(excelName);
//设置禁止弹出保存和覆盖的询问提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
app.UserControl = true
worksheet = (Excel.Worksheet)wb.Worksheets[1];
int col = 3;
int row = 1;
range = worksheet.Range[worksheet.Cells[row,col], worksheet.Cells[row,col]];
range.Value = string.Format("您好");
wb.Save();
app.Quit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
自己折中解决了。对于第一行列名,手动写入,而不是采用程序写入。用程序可以写入数据,但是不能写入列名,所以出错了。 展开
Excel.Workbooks wbs;
Excel.Workbook wb;
Excel.Worksheet worksheet;
Excel.Range range;
try
{
string excelName = "";
OpenFileDialog dlg = new OpenFileDialog();
dlg.ShowDialog();
excelName = dlg.FileName;
app = new Excel.Application();
wbs = app.Workbooks;
wb = wbs.Add(excelName);
//设置禁止弹出保存和覆盖的询问提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
app.UserControl = true
worksheet = (Excel.Worksheet)wb.Worksheets[1];
int col = 3;
int row = 1;
range = worksheet.Range[worksheet.Cells[row,col], worksheet.Cells[row,col]];
range.Value = string.Format("您好");
wb.Save();
app.Quit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
自己折中解决了。对于第一行列名,手动写入,而不是采用程序写入。用程序可以写入数据,但是不能写入列名,所以出错了。 展开
若以下回答无法解决问题,邀请你更新回答
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询