c# 把excel转换html,c# – 如何导入HTML格式的Excel

我已经使用HttpContext从数据库导出数据,格式为table,tr和td.我想读同一个文件并转换成datatable.

private DataTable GetTableFromExcel()

{

DataTable dt = new DataTable();

try

{

if (exclFileUpload.HasFile)

{

string FileName = Path.GetFileName(exclFileUpload.PostedFile.FileName);

string Extension = Path.GetExtension(exclFileUpload.PostedFile.FileName);

string FolderPath = Server.MapPath(ConfigurationManager.AppSettings["FolderPath"]);

//string NewFileName = string.Format("{0}_{1}",DateTime.Now.ToString().Replace("/","").Replace(" ","").Replace(":",""),FileName);

string FilePath = Path.Combine(string.Format("{0}/{1}",FolderPath,FileName));

exclFileUpload.SaveAs(FilePath);

string conStr = "";

switch (Extension)

{

case ".xls": //Excel 97-03

conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;

break;

case ".xlsx": //Excel 07

conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;

break;

}

conStr = String.Format(conStr,FilePath,true);

OleDbConnection connExcel = new OleDbConnection(conStr);

OleDbCommand cmdExcel = new OleDbCommand();

OleDbDataAdapter oda = new OleDbDataAdapter();

cmdExcel.Connection = connExcel;

connExcel.Open();

DataTable dtExcelSchema;

dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);

string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();

connExcel.Close();

connExcel.Open();

cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";

oda.SelectCommand = cmdExcel;

oda.Fill(dt);

connExcel.Close();

File.Delete(FilePath);

}

}

catch (Exception ex)

{

}

return dt;

}

当使用第二个连接字符串时,我收到错误“外部表不是预期格式的connection.Open()”.但是当使用第一个时,我在阅读工作表名称时会收到错误.

请告诉我如何从Excel中读取表格或直接从Excel中读取数据.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值