操作excel的一些方法
??
更改方法:
public void UpdateExcelFile(string filePath,string prjId, List<string> updateColNames,List<string> colValues)
? ? ? ? ? ?string s = System.IO.Path.GetExtension(filePath);
? ? ? ? ? ?string strConn = "";
? ? ? ? ? ?if (s.ToLower() == ".xls")
? ? ? ? ? ?{
? ? ? ? ? ? ? ?strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
? ? ? ? ? ?}
? ? ? ? ? else
? ? ? ? ? ?{
? ? ? ? ? ? ? ?strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;";
? ? ? ? ? ?}
? ? ? ? ? ?OleDbConnection conn = new OleDbConnection(strConn);
? ? ? ? ? ?conn.Open();
? ? ? ? ? ?string strExcel = "";
? ? ? ? ? ?//OleDbDataAdapter myCommand = null;
? ? ? ? ? ?//DataSet ds = null;
? ? ? ? ? ?StringBuilder sb = new StringBuilder();
? ? ? ? ? ?sb.Append("update [sheet1$] set ");
? ? ? ? ? ?sb.Append(updateColNames[0]);
? ? ? ? ? ?sb.Append("='");
? ? ? ? ? ?sb.Append(colValues[0]);
? ? ? ? ? ?sb.Append("'");
? ? ? ? ? ?for (int i = 1; i < updateColNames.Count; i++)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?sb.Append(",");
? ? ? ? ? ? ? ?sb.Append(updateColNames[i]);
? ? ? ? ? ? ? ?sb.Append("='");
? ? ? ? ? ? ? ?sb.Append(colValues[i]);
? ? ? ? ? ? ? ?sb.Append("'");
? ? ? ? ? ?}
? ? ? ? ? ?sb.Append(" where ProjectID=" + prjId + "");
? ? ? ? ? ?strExcel = sb.ToString();
? ? ? ? ? ?OleDbCommand cmd = null;
? ? ? ? ? ?cmd = new OleDbCommand(strExcel, conn);
? ? ? ? ? ?int row = cmd.ExecuteNonQuery();
? ? ? ? ? ?conn.Close();
? ? ? ?}
插入方法:
??public void InserDataExcelFile(string filePath,List<string> updateColNames, List<string> colValues)
? ? ? ?{
? ? ? ? ? ?string s = System.IO.Path.GetExtension(filePath);
? ? ? ? ? ?string strConn = "";
? ? ? ? ? ?if (s.ToLower() == ".xls")
? ? ? ? ? ?{
? ? ? ? ? ? ? ?strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
? ? ? ? ? ?}
? ? ? ? ? ?else
? ? ? ? ? ?{
? ? ? ? ? ? ? ?strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;";
? ? ? ? ? ?}
? ? ? ? ? ?OleDbConnection conn = new OleDbConnection(strConn);
? ? ? ? ? ?conn.Open();
? ? ? ? ? ?string strExcel = "";
? ? ? ? ? ?StringBuilder sb = new StringBuilder();
? ? ? ? ? ?sb.Append("insert into [sheet1$] ( ");
? ? ? ? ? ?sb.Append(updateColNames[0]);
? ? ? ? ? ?for (int i = 1; i < updateColNames.Count; i++)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?sb.Append(",");
? ? ? ? ? ? ? ?sb.Append(updateColNames[i]);
? ? ? ? ? ?}
? ? ? ? ? ?sb.Append(" ) values ( '");
? ? ? ? ? ?sb.Append(colValues[0]);
? ? ? ? ? ?sb.Append("'");
? ? ? ? ? ?for (int i = 1; i < colValues.Count; i++)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?sb.Append(",'");
? ? ? ? ? ? ? ?sb.Append(colValues[i]);
? ? ? ? ? ? ? ?sb.Append("'");
? ? ? ? ? ?}
? ? ? ? ? ?sb.Append(" )");
? ? ? ? ? ?strExcel = sb.ToString();
? ? ? ? ? ?OleDbCommand cmd = null;
? ? ? ? ? ?cmd = new OleDbCommand(strExcel, conn);
? ? ? ? ? ?cmd.ExecuteNonQuery();
? ? ? ? ? ?conn.Close();
? ? ? ?}
讀方法:
?public static System.Data.DataSet ExcelToDS(string filePath,string tableName)//讀Excel方法
? ? ? ? {
? ? ? ? ? ? string s = System.IO.Path.GetExtension(filePath);
? ? ? ? ? ? string strConn = "";
? ? ? ? ? ? if (s.ToLower() == ".xls")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;";
? ? ? ? ? ? }
? ? ? ? ? ? OleDbConnection conn = new OleDbConnection(strConn);
? ? ? ? ? ? conn.Open();
? ? ? ? ? ? string strExcel = "";
? ? ? ? ? ? OleDbDataAdapter myCommand = null;
? ? ? ? ? ? DataSet ds = null;
? ? ? ? ? ? strExcel = "select * from [sheet1$]";
? ? ? ? ? ? myCommand = new OleDbDataAdapter(strExcel, strConn);
? ? ? ? ? ? ds = new DataSet();
? ? ? ? ? ? myCommand.Fill(ds, tableName);
? ? ? ? ? ? return ds;
? ? ? ? }
遍歷dataset方法:
以MyDataset為例,其實與普通DataSet一樣 foreach (DataTable dt in MyDataset.Tables) //MyDataSet是自已定義并已賦值的DataSet對象。 { foreach (DataRow dr in dt.Rows) ///遍歷所有的行{foreach (DataColumn dc in dt.Columns) //遍歷所有的列{Console.WriteLine(“{0}, {1}, {2}”, dt.TableName, dc.ColumnName, dr[dc]); //表名,列名,單元格數據}} }//遍歷DataSet中第一個表的多行多列 foreach(DataRow mDr in MyDataset.Tables[0].Rows ) { foreach(DataColumn mDc in MyDataset.Tables[0].Columns) { Console.WriteLine(mDr[mDc].ToString()); } }
利用ASpose的dll刪除
using?System;
using?System.Collections.Generic;
using?System.Text;
using?Aspose.Cells;
namespace?ConsoleApplication12
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????if?(DeleteRow("D:\\SS.XLS",?"sheet1",?"0"))
????????????????Console.WriteLine("true");
????????????else
????????????????Console.WriteLine("false");
????????????Console.Read();
????????}
????????public?static?bool?DeleteRow(string?ExcelPath,?string?SheetName,?string?LineSNumber)
????????{
????????????bool?isDelete?=?false;
????????????try
????????????{
????????????????Workbook?workbook?=?new?Workbook();
????????????????workbook.LoadData(ExcelPath);
????????????????Worksheet?sheet?=?workbook.Worksheets[SheetName];
????????????????sheet.Cells.DeleteRow(Int32.Parse(LineSNumber));
????????????????workbook.Save(ExcelPath);
????????????????isDelete?=?true;
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????isDelete?=?false;
????????????}
????????????return?isDelete;
????????}
????}
}
總結
以上是生活随笔為你收集整理的操作excel的一些方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 连接excel执行Insert Into
- 下一篇: 科目三电子路考操作流程