C# Excel单元格数据插入问题
专家你好,我是用C#做编程!我有一个excel是从模版excel里面copy过来的xls文件,直接用File.Copy(sourcepath,destPath,true)...
专家你好,我是用C#做编程!我有一个excel是从模版excel里面copy过来的xls文件,直接用File.Copy(sourcepath, destPath, true),我现在要在指定的几行几列插入数据,怎么做到,谢谢
展开
1个回答
展开全部
OleDbDataAdapter+OleDbCommandBuilder+dataset
有什么数据修改/添加先在dataset完成,最后OleDbDataAdapter.update();方法搞定。
参考:
OleDbDataAdapter OleDbAderNew = new OleDbDataAdapter();
OleDbAderNew.SelectCommand = new OleDbCommand("SELECT * from [VTable]", con);//取模版
OleDbCommandBuilder ScbldNew = new OleDbCommandBuilder(OleDbAderNew);
int rok = 0;
try
{
con.Open();
DataSet temds = new DataSet();
DataTable temDataTable;
temDataTable = ds.Tables["Na107_lens"].Copy();
temds.Tables.Add(temDataTable);
for (int i = 0; i <= temDataTable.Rows.Count - 1; i++)
{
temDataTable.Rows[i]["列名"] = temDataTable.Rows[i]["列名"].ToString().Substring(0, 2)
+ (oldmonmax + i + 1).ToString().PadLeft(4, '0') + temDataTable.Rows[i]["列名"].ToString();
}
rok = OleDbAderNew.Update(temds, "table");
MessageBox.Show(rok.ToString() + "条数据导出成功 Excel!");
有什么数据修改/添加先在dataset完成,最后OleDbDataAdapter.update();方法搞定。
参考:
OleDbDataAdapter OleDbAderNew = new OleDbDataAdapter();
OleDbAderNew.SelectCommand = new OleDbCommand("SELECT * from [VTable]", con);//取模版
OleDbCommandBuilder ScbldNew = new OleDbCommandBuilder(OleDbAderNew);
int rok = 0;
try
{
con.Open();
DataSet temds = new DataSet();
DataTable temDataTable;
temDataTable = ds.Tables["Na107_lens"].Copy();
temds.Tables.Add(temDataTable);
for (int i = 0; i <= temDataTable.Rows.Count - 1; i++)
{
temDataTable.Rows[i]["列名"] = temDataTable.Rows[i]["列名"].ToString().Substring(0, 2)
+ (oldmonmax + i + 1).ToString().PadLeft(4, '0') + temDataTable.Rows[i]["列名"].ToString();
}
rok = OleDbAderNew.Update(temds, "table");
MessageBox.Show(rok.ToString() + "条数据导出成功 Excel!");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询