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 用于判断导入文件是否 ...
随机推荐
- 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Linux sed命令实例详解
简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...
- Objective-C:Foundation框架-常用类-NSString全解
Foundation框架中常用的类有字符串.集合.字典等,这里介绍字符串NSString.本文分别介绍了NSString的创建.从文件里读取NSString字符串.通过函数改变外部的NSString变 ...
- 详解Jquery和AngularJs,Servlet中jsonp解决跨域问题(转)
众所周知,jsonp可以解决跨域问题,下面是我在查阅资料和实际项目使用后的一些总结. Jquery中jsonp的使用 //myUrl = "http://localhost:8090/api ...
- urlencode在url中的作用
urlencode编码能解决特殊字符的传输问题. 使用urlencode主要用于正常识别输入的汉字.空格以及其他特殊字符. 列如: 一产品名称为A&T Plastic,在产品列表中就产生了这样 ...
- [转]无IDE时编译和运行Java
本文由 ImportNew - Grey 翻译自 dzone.欢迎加入Java小组.转载请参见文章末尾的要求. 最近 Java subreddit 出现了一篇”在没有IDE的情况下编译Java包” 的 ...
- 源码编译Chrome
官网描述 http://www.chromium.org/developers/how-tos/build-instructions-windows 为啥还要写这篇博客 太久没在这里写博客 Chrom ...
- 实现OAUTH协议 实现 QQ 第三方登录效果
1.OAuth的简述 OAuth(Open Authorization,开放授权)是为用户资源的授权定义了一个安全.开放及简单的标准,第三方无需知道用户的账号及密码,就可获取到用户的授权信息,并且这是 ...
- js基础之BOM
一.window.open 栗子:阿里西西运行代码功能 var oBtn = document.getElementById('btn1'); var oTxt = document.getEleme ...
- GCJ 2015-Qualification-B Infinite House of Pancakes 枚举,思路,误区 难度:3
https://code.google.com/codejam/contest/6224486/dashboard#s=p1 题目不难,教训记终生 题目给了我们两种操作:1 所有人都吃一个,简记为消除 ...