資源簡介
通用類,專門用來操作Excel .包含 讀取 導出 寫入 等功能
代碼片段和文件信息
//C#讀取Excel
?
private?void?ReadExcel(){
?
? ?????OleDbCommand?cmd?=?null;
????????????OleDbConnection?conn?=?null;
????????????string?fileName?=?hidFileName.Text?+?“.xls“;
????????????string?filePath?=?Path.Combine(Server.MapPath(“Excell/ImportExcelTempFolder“)?fileName);
????????????try
????????????{
????????????????String?connString?=?“Provider=Microsoft.Jet.OLEDB.4.0;“?+
??????????????????“Data?Source=“?+?filePath?+?“;Extended?Properties=\“Excel?8.0;HDR=YES;IMEX=1\““;
?
????????????????string?message?=?““;
????????????????int?count?=?0;
????????????????using?(conn?=?new?OleDbConnection(connString))
????????????????{
????????????????????if?(conn.State?==?ConnectionState.Closed)
????????????????????????conn.Open();
????????????????????cmd?=?conn.CreateCommand();
????????????????????string?strSql?=?“SELECT?[老師]?[課程][年級][班級][合作老師]??FROM?[授課安排$]“;
????????????????????cmd.CommandText?=?strSql;
????????????????????using?(OleDbDataReader?dr?=?cmd.ExecuteReader(CommandBehavior.CloseConnection))
????????????????????{
????????????????????????int?i?=?0;
????????????????????????while?(dr.Read())
????????????????????????{
????????????????????????????i++;
?
????????????????????????????......
?
?????????????????????????}
?
???}
?
}
?
???????????}
?
?catch?(Exception?ex)
????????????{
????????????????if?(File.Exists(filePath))
????????????????{
????????????????????File.Delete(filePath);
????????????????}
????????????????throw?ex;
????????????}
????????????finally
????????????{
????????????????if?(conn?!=?null?&&?conn.State?==?ConnectionState.Open)
????????????????????conn.Close();
????????????}
?
}
?
//C#導出為Excel
?
//這里我寫了一個通用類,專門用來操作Excel?
?
?
?
using?System;
using?System.IO;
using?System.Data;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Text;
using?System.Globalization;
using?System.Collections;
using?System.Data.OleDb;
?
namespace?Com.DRPENG.SDXY.UI.Common
{
????public?class?ExcelHelper
????{
????????static?object?obj?=?new?object();
????????#region?Fields
????????string?_fileName;
????????DataTable?_dataSource;
????????string[]?_titles?=?null;
????????string[]?_fields?=?null;
????????int?_maxRecords?=?1000;
?
????????#endregion
?
????????#region?Properties
?
????????///??
????????///?限制輸出到?Excel?的最大記錄數。超出則拋出異常?
????????///? ?
????????public?int?MaxRecords
????????{
????????????set?{?_maxRecords?=?value;?}
????????????get?{?return?_maxRecords;?}
????????}
?
????????///??
????????///?輸出到瀏覽器的?Excel?文件名?
????????///? ?
????????public?string?FileName
????????{
????????????set?{?_fileName?=?value;?}
????????????get?{?return?_fileName;?}
????????}
?
????????#endregion
?
????????#region?.ctor
?
????????///??
????????///?構造函數?
????????///? ?
????????///?tles“>要輸出到?Excel?的列標題的數組?
????????///?要輸出到
評論
共有 條評論