Excel檔案讀寫

2021-04-18 06:54:12 字數 2481 閱讀 5220

對於c/s模式的開發,讀寫excel檔案是很經常的事情,這裡我介紹一下通過oledb方式對excel檔案進行讀寫的方法,相比通過操作單元格的方式,應該在效能和效率上有很大優勢。

首先、提供乙個excel檔案操作引擎類。

using system;

using system.data;

using system.data.oledb;

using system.componentmodel;

using system.componentmodel.design;

public class fileengineexcel : system.componentmodel.component

#endregion

#region 構造方法

public fileengineexcel()

public fileengineexcel(system.componentmodel.icontainer container) : this()

#endregion

#region 銷毀方法

protected override void dispose(bool disposing)

base.dispose (disposing);

}#endregion

#region 開啟關閉excel檔案

public void open(string filename, bool readonly)

public void open(string filename)

public void close()

#endregion

#region 表(sheet)操作

public void createtable(string table, string columnnames)

public bool istableexist(string table)

catch

return true;

}public void droptable(string table)

public string gettableschema(string table)

#endregion

#region 按行插入資料

public void begininsert(string table, string cols)

public void begininsert(string table)

public void insert(object values)

#endregion

#region 按行讀取資料

public idatareader getdatareader(string table, string cols)

else

cmd.commandtext = cmdtxt + " from " + table;

return cmd.executereader();

}public idatareader getdatareader(string table)

#endregion

#region 匯出excel擴充套件

public void createtable(string table, string columnnames,string types)

cmd.commandtext = cmdtxt + ")";

cmd.executenonquery();

}public void begininsert(string table, string cols,string types)

}public string gettabletype(datatable table)

public void insertval(object values)

cmd.executenonquery();

}private string getfieldtype(string type)

private oledbtype getfiledoletype(string type)

private object getvalue(object val)

else if (val is decimal)

else

}else

return system.dbnull.value;

}#endregion

}其次、提供匯入excel檔案示例。

private void importexcel()

}finally

再次、提供匯出excel檔案示例。

private static bool exporttoxls(datatable table, string filename)

engine.insertval(table.defaultview[i].row.itemarray);}}

catch(exception ex)

finally

return true;

}

讀寫Excel檔案

先要引用這些命名空間.不明白的地方可以在msdn中找到.using system.data.oledb using excel using system.reflection for missing.value and bindingflags 讀取excel excel檔案全名 放到dataset...

Android 讀寫Excel檔案

android 讀寫 excel 檔案 需求背景 最近在做專案過程中,需要讀取 excel 檔案,excel檔案可以來自使用者插在android裝置上的外接u盤,也可以是儲存在專案assets raw裡面。資料參考 查閱了很多相關資料,讀取外接u盤主要用到了android 讀取usb檔案的第三方開源...

python讀寫excel檔案

開啟excel檔案讀取資料 data xlrd.open workbook jinrong.xlsx 獲取工作表 table data.sheet by name name tabel data.sheets 0 tabel data.sheet by index 0 獲取行數和列數 nrows t...