Excel追加数据问题!!!

鬼五拾柒 2015-04-16 09:23:44
在不保存Excel文件的情况下向Excel文件添加一列数据,这个咋整。
现在添加数据已经写好,就是每次添加原来那一列数据都清除了,只会添加新的一列数据

string path = string.Format(@"{0}\", this.textBox1.Text);
if (File.Exists(path + "samples.xls"))
{

}
else
{
StreamWriter test = new StreamWriter(path + "samples.xls", true);
}
if (IsFirstLoad == true)
{
Workbooks wbks = excel.Workbooks; //创建工作簿
_Workbook _wbk = wbks.Open(path + "samples.xls");//打开Excel
excel.DisplayAlerts = true;
Sheets shs = _wbk.Sheets;
Worksheet _wsh = (Worksheet)shs.get_Item(1);//取得Sheet
excel.Visible = true;
string cell1 = this.textBox5.Text.Trim(); ;
string cell2 = this.textBox6.Text.Trim();
string cell3 = this.textBox10.Text.Trim();
string c1 = cell1.Substring(0, 1);
int c11 = Convert.ToInt32(cell1.Substring(1));
if (this.textBox5.Text != null && this.textBox5.Text.Trim() != "")
{
for (int r = 1; r <= dataGridView1.Rows.Count; r++)
{
Microsoft.Office.Interop.Excel.Range cel = _wsh.get_Range(c1 + c11++);
cel.Value2 = dataGridView1.Rows[r - 1].Cells[2].Value.ToString();
}
}
if (this.textBox6.Text.Trim() != null && this.textBox6.Text.Trim() != "")
{
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
Microsoft.Office.Interop.Excel.Range cel = _wsh.get_Range(c1 + c11++);
excel.Cells[r + 1, 2] = dataGridView1.Rows[r].Cells[3].Value.ToString();
}
}
if (this.textBox10.Text.Trim() != null && this.textBox10.Text.Trim() != "")
{
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
Microsoft.Office.Interop.Excel.Range cel = _wsh.get_Range(c1 + c11++);
excel.Cells[r + 1, 3] = dataGridView1.Rows[r].Cells[4].Value.ToString();
}
}
IsFirstLoad = true;
}
else
{
Workbooks wbks = excel.Workbooks; //创建工作簿
excel.DisplayAlerts = false;
_Workbook _wbk = wbks.Open(path + "samples.xls");//打开Excel
excel.DisplayAlerts = true;
Sheets shs = _wbk.Sheets;
Worksheet _wsh = (Worksheet)shs.get_Item(1);//取得Sheet
excel.Visible = true;
string cell1 = this.textBox5.Text.Trim(); ;
string cell2 = this.textBox6.Text.Trim();
string cell3 = this.textBox10.Text.Trim();
string c1 = cell1.Substring(0, 1);
int c11 = Convert.ToInt32(cell1.Substring(1));
if (this.textBox5.Text != null && this.textBox5.Text.Trim() != "")
{
for (int r = 1; r <= dataGridView1.Rows.Count; r++)
{
Microsoft.Office.Interop.Excel.Range cel = _wsh.get_Range(c1 + c11++);
cel.Value2 = dataGridView1.Rows[r - 1].Cells[2].Value.ToString();
}
}
if (this.textBox6.Text.Trim() != null && this.textBox6.Text.Trim() != "")
{
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
Microsoft.Office.Interop.Excel.Range cel = _wsh.get_Range(c1 + c11++);
excel.Cells[r + 1, 2] = dataGridView1.Rows[r].Cells[3].Value.ToString();
}
}
if (this.textBox10.Text.Trim() != null && this.textBox10.Text.Trim() != "")
{
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
Microsoft.Office.Interop.Excel.Range cel = _wsh.get_Range(c1 + c11++);
excel.Cells[r + 1, 3] = dataGridView1.Rows[r].Cells[4].Value.ToString();
}
}
IsFirstLoad = true;
}
...全文
89 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
鬼五拾柒 2015-04-16
  • 打赏
  • 举报
回复
引用 1 楼 FoxDave 的回复:
没看明白你问的是啥,可以在通俗点儿吗?
我要往Excel这个模板文件里面追加数据,并保存前几次添加的数据
Justin-Liu 2015-04-16
  • 打赏
  • 举报
回复
没看明白你问的是啥,可以在通俗点儿吗?

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧