C 中Excel的匯入匯出

2021-10-09 08:26:55 字數 2720 閱讀 1366

重點:如果計算機沒裝excel,則必須安裝accessdatabaseengine_32.exe或者accessdatabaseengine_64.exe引擎,這樣電腦就安裝excel環境,匯入匯出就可以執行而不報錯了。(我的資源有這兩個檔案)

//button匯出

private void btnserch_click(object sender, eventargs e)

+ "\\體檢結果.xls");

"匯出成功! " + this.btnserch.text + "\\體檢結果.xls");

}//button匯入

private void ******button1_click(object sender, eventargs e)

//對原有資料清空

grd.datasource = null;

grv.columns.clear();

grd.mainview.populatecolumns();

openfiledialog openfile = new openfiledialog();

if (openfile.showdialog() == dialogresult.ok)

}

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.data;

using system.windows.forms;

using system.data.oledb;

using system.io;

namespace utils

); //得到所有sheet的名字

string firstsheetname = sheetsname.rows[0][2].tostring(); //得到第乙個sheet的名字

string sql = string.format("select * from ", firstsheetname); //查詢字串

//string sql = string.format("select * from where [日期] is not null", firstsheetname); //查詢字串

oledbdataadapter ada = new oledbdataadapter(sql, connstring);

dataset set = new dataset();

ada.fill(set);

datatable dt = new datatable();

if (set.tables[0] != null && set.tables[0].rows.count > 0)

for (int i = 1; i < set.tables[0].rows.count; i++)

}return dt;}}

catch (exception)

}public const string oledbconnstring = "provider = microsoft.ace.oledb.12.0 ; data source = ;extended properties='excel 8.0;hdr=yes;imex=1;'"; //excel

/// /// 獲取excel檔案中的資訊,儲存到乙個datatable中

///

/// 檔案路徑

/// 返回生成的datatable

public static datatable getexceltable(string path)

]", dt.rows[0]["table_name"]);

oledbdataadapter oleda = new oledbdataadapter(selsqlstr, conn);

oleda.fill(dt1);

}conn.close();

return dt1;

}catch (exception ex)

}/// /// 將excel匯入dataset

///

/// 檔案路徑

/// 頁名

/// 如果返回null表示錯誤

public static dataset excelttods(string path, string sheetname, string officeversion)

strexcel = "select * from [" + sheetname + "$]";

tryfinally

}/// /// 讀取excle檔案中的表單名稱,在返回的table中欄位名稱為"table_name"

///

/// excal檔案路徑

/// exca版本號

///

public static datatable getexcelsheetnames(string file, string officeversion)

tryfinally}}}

Java中Excel的匯入匯出

一.excel匯入 先在前台頁面定義乙個獲取excel檔案路徑的input 然後把這個路徑傳到後台類中 string excel request.getparameter excel workbook wb null try catch biffexception e catch ioexcepti...

excel匯入匯出

匯出 yii框架中 header content disposition attachment filename 123.xls arr connection createcommand select from recruit queryall array this db get recruit r...

EXCEL匯入匯出

系統管理員登入,系統下資源管理 分機號列表內容匯出 suppresswarnings rawtypes responsebody public jsonobject selectfenjihaoinxitongforexportbyparam requestparam value fenjihao ...