Excel文件的导出操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Data;
using YTO.WeiXin.Model; namespace YTO.WeiXin.Core
{
public class ExcelToDB
{
public HSSFWorkbook hssfworkbook;
//将excel文件转换成list
public IList<ContactInfo> ExcelToList(string path)
{
IList<ContactInfo> list = new List<ContactInfo>();
try
{
using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
{
hssfworkbook = new HSSFWorkbook(file);
HSSFSheet sheet = hssfworkbook.GetSheetAt() as HSSFSheet;
for (int i = ; i <= sheet.LastRowNum; i++)
{
HSSFRow row = sheet.GetRow(i) as HSSFRow;
ContactInfo contactInfo = new ContactInfo();
contactInfo.Id = Guid.NewGuid().ToString();
if (row.GetCell() != null)
{
row.GetCell().SetCellType(CellType.STRING);
contactInfo.CenterName = row.GetCell().StringCellValue.ToString();
}
else
{
contactInfo.CenterName = "";
}
if (row.GetCell() != null)
{
row.GetCell().SetCellType(CellType.STRING);
contactInfo.Name = row.GetCell().StringCellValue.ToString();
}
else
{
contactInfo.Name = "";
}
if (row.GetCell() != null)
{
row.GetCell().SetCellType(CellType.STRING);
contactInfo.PhoneNumber = row.GetCell().StringCellValue.ToString();
}
else
{
contactInfo.PhoneNumber = "";
}
if (row.GetCell() != null)
{
row.GetCell().SetCellType(CellType.STRING);
contactInfo.Address = row.GetCell().StringCellValue.ToString();
}
else
{
contactInfo.Address = "";
}
list.Add(contactInfo);
}
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
//中心联系方式导出
public MemoryStream ExportToExcel(string fileName, IList<ContactInfo> list)
{
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet1 = workbook.CreateSheet("Sheet1");
sheet1.SetColumnWidth(, * );
sheet1.SetColumnWidth(, * );
sheet1.SetColumnWidth(, * );
sheet1.SetColumnWidth(, * );
Row row = sheet1.CreateRow();
row.HeightInPoints = ;
row.CreateCell().SetCellValue("中心名称");
row.CreateCell().SetCellValue("联系人");
row.CreateCell().SetCellValue("联系方式");
row.CreateCell().SetCellValue("地址");
CellStyle style = workbook.CreateCellStyle();
style.Alignment = HorizontalAlignment.CENTER;
style.WrapText = true;
Font font = workbook.CreateFont();
font.FontHeightInPoints = ;
font.Boldweight = (short)FontBoldWeight.BOLD;
font.Color = (short)FontColor.RED;
style.SetFont(font);
for (int i = ; i < ; i++)
{
row.GetCell(i).CellStyle = style;
} for (int i = ; i < list.Count; i++)
{
row = sheet1.CreateRow(i);
row.CreateCell().SetCellValue(list[i - ].CenterName);
row.CreateCell().SetCellValue(list[i - ].Name);
row.CreateCell().SetCellValue(list[i - ].PhoneNumber);
row.CreateCell().SetCellValue(list[i - ].Address);
}
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
ms.Flush();
ms.Position = ;
return ms;
} //异常信息导出
public MemoryStream ExportExcptionToExcel(string fileName, IList<ExcptionInfo> list)
{
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet1 = workbook.CreateSheet("Sheet1");
sheet1.SetColumnWidth(, * );
sheet1.SetColumnWidth(, * );
sheet1.SetColumnWidth(, * );
sheet1.SetColumnWidth(, * );
sheet1.SetColumnWidth(, * );
sheet1.SetColumnWidth(, * );
sheet1.SetColumnWidth(, * );
Row row = sheet1.CreateRow();
row.HeightInPoints = ;
row.CreateCell().SetCellValue("车牌号");
row.CreateCell().SetCellValue("线路");
row.CreateCell().SetCellValue("手机号");
row.CreateCell().SetCellValue("异常情况");
row.CreateCell().SetCellValue("异常类型");
row.CreateCell().SetCellValue("位置");
row.CreateCell().SetCellValue("上报时间");
CellStyle style = workbook.CreateCellStyle();
style.Alignment = HorizontalAlignment.CENTER;
style.WrapText = true;
Font font = workbook.CreateFont();
font.FontHeightInPoints = ;
font.Boldweight = (short)FontBoldWeight.BOLD;
font.Color = (short)FontColor.RED;
style.SetFont(font);
for (int i = ; i < ; i++)
{
row.GetCell(i).CellStyle = style;
}
for (int i = ; i < list.Count; i++)
{
row = sheet1.CreateRow(i);
row.CreateCell().SetCellValue(list[i].LiencePlateNumber);
row.CreateCell().SetCellValue(list[i].CarLine);
row.CreateCell().SetCellValue(list[i].PhoneNumber);
row.CreateCell().SetCellValue(list[i].Remark);
row.CreateCell().SetCellValue(list[i].ExcptionCategory);
row.CreateCell().SetCellValue(list[i].Position);
row.CreateCell().SetCellValue(list[i].CreateTime.ToString());
}
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
ms.Flush();
ms.Position = ;
return ms;
}
}
}
Excel文件的导出操作的更多相关文章
- c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出
c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...
- c# .Net :Excel NPOI导入导出操作教程之List集合的数据写到一个Excel文件并导出
将List集合的数据写到一个Excel文件并导出示例: using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using System;using Sys ...
- springMVC(4)---生成excel文件并导出
springMVC(4)---生成excel文件并导出 在开发过程中,需要将数据库中的数据以excel表格的方式导出. 首先说明.我这里用的是Apache的POI项目,它是目前比较成熟的HSSF接口, ...
- NodeJs之EXCEL文件导入导出MongoDB数据库数据
NodeJs之EXCEL文件导入导出MongoDB数据库数据 一,介绍与需求 1.1,介绍 (1),node-xlsx : 基于Node.js解析excel文件数据及生成excel文件. (2),ex ...
- Excel文件导入导出(基于Nodejs、exceljs)
Excel导入.导出是大多数项目的管理后台必备功能.几年来使用过多个该功能的实现包,最近一次开发该功能,突然发现一个人气极高(3000+)的包,这里记录一下使用方法. 大凡厉害的技术的文档咋一看都想字 ...
- Excel导入导出工具(简单、好用且轻量级的海量Excel文件导入导出解决方案.)
Excel导入导出工具(简单.好用且轻量级的海量Excel文件导入导出解决方案.) 置顶 2019-09-07 16:47:10 $9420 阅读数 261更多 分类专栏: java 版权声明:本 ...
- 用NODEJS处理EXCEL文件导入导出,文件上传
參考文章 http://librajt.github.io/2013/08/04/handle-excel-file-with-nodejs/ 对照了 ExcelJS ,https://github. ...
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
导出到Excel方法: <span style="color: rgb(0, 0, 255);">public</span> <span style= ...
- Excel文件导入导出
/** * 导入Excel文件数据 * * @param file 将要导入的Excel文件 * @param fileCheckKeyWord 用于判断导入文件是否 ...
随机推荐
- ztree check
<link rel="stylesheet" href="${contextPath}/resources/ztree/css/demo.css" typ ...
- webService—调用webService
Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...
- oracle序列为什么不是从1开始
问题原因: ·当我们使用序列作为插入数据时,如果使用了“延迟段”技术,则跳过序列的第一个值 ·Oracle从 11.2.0.1版本开始,提供了一个“延迟段创建”特性: 即 当我们创建了新的表(tabl ...
- JFrame面板
1.可见性与透明性 可见性:当面板不可见时,则该面板中包含的组件会无法显示. 透明性:若该面板是可见且透明的,那么只是面板会透明(比如面板的背景色无法看到),面板上的组件仍会显示. 注:可见性通过se ...
- uva 1639--精度处理方法之取对数(uva 1639)
1639 - Candy Time limit: 3.000 seconds 1639 CandyLazyChild is a lazy child who likes candy very much ...
- jQuery性能优化指南(转载)
现在jquery应用的越来越多, 有些同学在享受爽快淋漓coding时就将性能问题忽略了, 比如我. jquery虽在诸多的js类库中性能表现还算优秀, 但毕竟不是在用原生的javascript开发, ...
- 生成json对象
JSONObject 对于放入的object,最终生成的json是什么样的? 两个JavaBean: public class ClassBean { private int grade; priva ...
- 时间序列数据库选型——本质是列存储,B-tree索引,抑或是搜索引擎中的倒排索引
时间序列数据库最多,使用也最广泛.一般人们谈论时间序列数据库的时候指代的就是这一类存储.按照底层技术不同可以划分为三类. 直接基于文件的简单存储:RRD Tool,Graphite Whisper.这 ...
- git的一个merge流程
git merge testSupport 合并testSupport分支代码到当前分支. 若无冲突发生,git commit -m "RM ID:5094",在git push即 ...
- BZOJ3888 [Usaco2015 Jan]Stampede
我们只要把每头牛开始遮挡视线和结束遮挡视线的时间点都搞出来就好= = 再按照y轴排序...然后变成线段覆盖了..线段树搞一下就好了? /******************************** ...