c#

位置:IT落伍者 >> c# >> 浏览文章

C#中导入导出Excel的操作


发布日期:2022年09月09日
 
C#中导入导出Excel的操作

/// <summary>

/// 读取Excel文档

/// </summary>

/// <param name=Path>文件名称</param>

/// <returns>返回一个数据集</returns>

public DataSet ExcelToDS(string Path)

{

string strConn = Provider=MicrosoftJetOLEDB; +Data Source=+ Path +;+Extended Properties=Excel ;;

OleDbConnection conn = new OleDbConnection(strConn);

connOpen();

string strExcel = ;

OleDbDataAdapter myCommand = null;

DataSet ds = null;

strExcel=select * from [sheet$];

myCommand = new OleDbDataAdapter(strExcel strConn);

ds = new DataSet();

myCommandFill(dstable);

return ds;

}

/// <summary>

/// 写入Excel文档

/// </summary>

/// <param name=Path>文件名称</param>

public bool SaveFPtoExcel(string Path)

{

try

{

string strConn = Provider=MicrosoftJetOLEDB; +Data Source=+ Path +;+Extended Properties=Excel ;;

OleDbConnection conn = new OleDbConnection(strConn);

connOpen();

SystemDataOleDbOleDbCommand cmd=new OleDbCommand ();

cmdConnection =conn;

//cmdCommandText =UPDATE [sheet$] SET 姓名= WHERE 工号=日期;

//cmdExecuteNonQuery ();

for(int i=;i<fpSheets []RowCount ;i++)

{

if(fpSheets []Cells[i]Text!=)

{

cmdCommandText =INSERT INTO [sheet$] (工号姓名部门职务日期时间) VALUES(+fpSheets []Cells[i]Text+ +

fpSheets []Cells[i]Text++fpSheets []Cells[i]Text++fpSheets []Cells[i]Text+

+fpSheets []Cells[i]Text++fpSheets []Cells[i]Text+);

cmdExecuteNonQuery ();

}

}

connClose ();

return true;

}

catch(SystemDataOleDbOleDbException ex)

{

SystemDiagnosticsDebugWriteLine (写入Excel发生错误+exMessage );

}

return false;

}               

上一篇:C# 实现的多线程异步Socket数据包接收器

下一篇:C# 开发和使用中的23个技巧