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 用于判断导入文件是否 ...
随机推荐
- SAP 质检使用非物料基本单位
今天也是奇葩了,物料A基本单位平方米,转化单位卷,销售和采购是按照平方米来采购,但是质检的需要按照平方米来做抽样检验.... 程序开发的: 修改该物料的工序基本单位为卷,再修改检验计划基本单位为卷 程 ...
- Linux下lzop命令安装
[root@xd502djj ~]# yum install lzop Loaded plugins: fastestmirror Determining fastest mirrors * base ...
- Hive的Security配置
为了更好地使用好Hive,我将<Programming Hive>的Security章节取出来,翻译了一下. Hive还是支持相当多的权限管理功能,满足一般数据仓库的使用. Hive由一个 ...
- easyui 中重复加载两次url
之前一直在使用easyui中,忽视了官网上的小细节,类似于datagrid.combobox 等组件在使用的时候,它的数据加载方式分为两种: 官网中: ①在html中,比如: <table id ...
- YanYan Self Introduction
My Website: http://finehappy.com/
- bzoj 1877: [SDOI2009]晨跑
#include<cstdio> #include<iostream> #include<cstring> #define M 6009 #define inf 2 ...
- Codeforces Round #257 (Div. 1) (Codeforces 449D)
思路:定义f(x)为 Ai & x==x 的个数,g(x)为x表示为二进制时1的个数,最后答案为 .为什么会等于这个呢:运用容斥的思想,如果 我们假设 ai&x==x 有f(x ...
- C#保存登录用户名供其他页面调用
一.保存登录用户名供其他页面调用 步骤: (1)项目自带的Program.cs,类方法里定义登录的用户名为全局变量loginid,这样整个项目都可以调用它 static class Program { ...
- node cheerio
所谓工欲善其事,必先利其器,所以通读了cheerio的API,顺便翻译了一遍,有些地方因为知道的比较少,不知道什么意思,保留了英文,希望各位不吝告诉我,然后一起把这个翻译完成. ###cheerio ...
- 在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案
转自:http://www.jianshu.com/p/89687f618837 原因分析 当我们在Android依赖库中使用switch-case语句访问资源ID时会报如下图所示的错误,报的错误 ...