当前位置:网站首页>ASP.NET 获取数据库的数据并写入到excel表格中
ASP.NET 获取数据库的数据并写入到excel表格中
2022-08-04 00:47:00 【CSDN问答】
问题遇到的现象和发生背景
从数据库读取出数据,并且把数据写入到excel表格对应的列中
问题相关代码,请勿粘贴截图
string fileDir = System.Web.HttpContext.Current.Server.MapPath("~/UI/BQ/Temp/测试报表.xlsx"); FileStream file = new FileStream(fileDir, FileMode.Open, FileAccess.Read); //创建HSSFWorkbook对象 XSSFWorkbook hssfworkbook = new XSSFWorkbook(file); //创建HSSFSheet对象 NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0); //ISheet sheet = hssfworkbook.GetSheet("sheet1"); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); DataTable dataTable; CustomSqlSection customSqlSection = Gateway.Default.FromCustomSql(sql); dataTable = customSqlSection.ToDataSet().Tables[0]; for (int i = 2; i < dataTable.Rows.Count; i++) { sheet.GetRow(i).GetCell(0).SetCellValue(dataTable.Rows[i - 2]["VIN"].ToString()); sheet.GetRow(i).GetCell(1).SetCellValue(dataTable.Rows[i - 2]["CAR_TYPE_CODE"].ToString()); sheet.GetRow(i).GetCell(2).SetCellValue(dataTable.Rows[i - 2]["BAD_DESC"].ToString()); sheet.GetRow(i).GetCell(3).SetCellValue(dataTable.Rows[i-2]["Model"].ToString()); sheet.GetRow(i).GetCell(4).SetCellValue(dataTable.Rows[i - 2]["LEVEL_VAL"].ToString()); sheet.GetRow(i).GetCell(5).SetCellValue(dataTable.Rows[i - 2]["HD2"].ToString()); sheet.GetRow(i).GetCell(6).SetCellValue(dataTable.Rows[i - 2]["OTHER_DES"].ToString()); }
运行结果及报错内容
写入的时候这个循环体内的数据报错: sheet.GetRow(i).GetCell(0).SetCellValue(dataTable.Rows[i - 2]["VIN"].ToString());//
{"EXCEPTION":"文件写入数据异常:未将对象引用设置到对象的实例。"}
我的解答思路和尝试过的方法
给表格增加列名
我想要达到的结果
从数据库读取出数据,并且把数据写入到excel表格对应的列中
边栏推荐
- 114. How to find the cause of Fiori Launchpad routing error by single-step debugging
- Web3 security risks daunting?How should we respond?
- 共享新能源充电桩充电站建设需要些什么流程及资料?
- Talking about the future development direction of my country's industrial parks
- 分子个数 数论(欧拉函数 前缀和
- What warehouse management problems can WMS warehouse management system solve in the electronics industry?
- typescript52-简化泛型函数调用
- typescript48 - type compatibility between functions
- "Miscellaneous" barcode by Excel as a string
- 易动纷享--测试实习生视频面试
猜你喜欢
【面经】被虐了之后,我翻烂了equals源码,总结如下
一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
微服务的简单介绍
电子组装行业对MES管理系统的需求分析
typescript55-泛型约束
Talking about the future development direction of my country's industrial parks
2015年开源大事件汇总
How to find the cause of Fiori Launchpad routing errors by single-step debugging
Shell编程之循环语句(for、while)
R3LIVE论文学习(二):VIO子系统
随机推荐
电子组装行业对MES管理系统的需求分析
jmeter分布式压测
新一代服务网关Gateway的实践笔记
越来越火的图数据库到底能做什么?
Sqlnet. Ora file with the connection of authentication test
C # WPF equipment monitoring software (classic) - the next
WMS仓储管理系统能解决电子行业哪些仓库管理问题
typescript53 - generic constraints
【链路聚合原理及配置】
Salesforce的中国区业务可能出现新变化,传言可能正在关闭
分布式事务框架 seata
ES6高级-迭代器与生成器的用法
LeetCode 19:删除链表的倒数第 N 个结点
建木DevOps流程的快速运用
Shell编程之循环语句(for、while)
C# wpf使用ffmpeg命令行实现录屏
Apple told Qualcomm: I bought a new campus for $445 million and may plan to speed up self-development of baseband chips
扩展卡尔曼滤波EKF
win10+cuda11.7+pytorch1.12.0 installation
卡尔曼滤波器KF