推扬网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
推扬网 门户 你问我答 查看内容

C# Excel to Database(SQL) 一起帮忙解决难题,拯救 IT 人的一天

2021-7-26 21:41| 发布者: admin| 查看: 442| 评论: 0|原作者: admin|来自: 推杨站长网

摘要: 我想請問一下,Excel如果要上傳到Database,需要先轉成 DataTable嗎? 我查了一些資料都是先 Excel → DataTable → Database(sql) 我對DataTabl...

我觉得请问一下,Excel假如要上传入Database,必须先转为 DataTable吗?
我搜了一些材料全是先 Excel → DataTable → Database(sql)

我对DataTable并并不是很熟,这几天逐渐学,我想问一下有别的方式 吗?
如果有别的方式 请要我参照,麻烦了,感谢

下列就是我读Excel to DataTable
但是为了更好地确定,用Console.WriteLine读出是错码
模块是应用 NPOI

using System; using System.Data; using System.IO; using System.Text; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; namespace Excel_to_DataTable { class Program { static void Main(string[] args) { DataTable dt = new DataTable(); using(FileStream file = new FileStream(@"H:\ying\桌面上\test\LIST.xlsx", FileMode.Open,FileAccess.Read)) { IWorkbook workbook = null; workbook = new XSSFWorkbook(file); ISheet sheet = workbook.GetSheetAt(0); IRow row; row = sheet.GetRow(0); if (row != null) { for(int m = 0; m < row.LastCellNum; m ) { string cellvalue = row.GetCell(m).ToString(); dt.Columns.Add(cellvalue); } } for(int i = 1; i <= sheet.LastRowNum; i ) { System.Data.DataRow dr = dt.NewRow(); row = sheet.GetRow(i); if(row != null) { for(int j=0; j < row.LastCellNum; j ) { string cellvalue = row.GetCell(j).ToString(); dr[j] = cellvalue; } } dt.Rows.Add(dr); } file.Close(); foreach(DataRow roww in dt.Rows) { foreach (DataColumn col in dt.Columns ) { Console.WriteLine(roww[col]); } } Console.ReadLine(); } } }

}


鲜花

握手

雷人

路过

鸡蛋

最新评论

热门推荐
最新资讯

广告服务|投稿要求|禁言标准|版权说明|免责声明|手机版|小黑屋|推扬网 ( 粤ICP备18134897号 )|网站地图 | 邮箱:vayae@hotmail.com

GMT+8, 2024-3-19 11:14 , Processed in 0.073284 second(s), 28 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

返回顶部