将列添加到现有的Excel文件 [英] Add columns to exiting Excel file
本文介绍了将列添加到现有的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Excel文件(excelFile.xls).如何将新列追加到现有excel文件中的最后一列.
谢谢,
这是我的代码
public void AddColumn(string sheetName, string strConn) { try { OleDbConnection oledbConn = new OleDbConnection(strConn); oledbConn.Open(); string strQuery = string.Format("SELECT * FROM [" + sheetName + "]", oledbConn); OleDbDataAdapter oledbDA = new OleDbDataAdapter(strQuery, strConn); DataSet DS = new DataSet(); oledbDA.Fill(DS); DataColumn addColumn = new DataColumn(); addColumn.DataType = System.Type.GetType("System.String"); addColumn.ColumnName = "Test"; addColumn.ReadOnly = true; addColumn.AllowDBNull = true; DS.Tables[0].Columns.Add(addColumn); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
解决方案
请参阅以下链接: ^ ]
>
Hi,
I have an excel file (excelFile.xls). How can I append new columns to the last column in existing excel file.
thanks,
Here are my code
public void AddColumn(string sheetName, string strConn) { try { OleDbConnection oledbConn = new OleDbConnection(strConn); oledbConn.Open(); string strQuery = string.Format("SELECT * FROM [" + sheetName + "]", oledbConn); OleDbDataAdapter oledbDA = new OleDbDataAdapter(strQuery, strConn); DataSet DS = new DataSet(); oledbDA.Fill(DS); DataColumn addColumn = new DataColumn(); addColumn.DataType = System.Type.GetType("System.String"); addColumn.ColumnName = "Test"; addColumn.ReadOnly = true; addColumn.AllowDBNull = true; DS.Tables[0].Columns.Add(addColumn); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
解决方案
Refer the link at: http://office.microsoft.com/en-us/excel-help/add-or-remove-table-rows-and-columns-in-an-excel-table-HA010013765.aspx[^]
这篇关于将列添加到现有的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文