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文件的导出操作的更多相关文章

  1. c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出

    c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...

  2. c# .Net :Excel NPOI导入导出操作教程之List集合的数据写到一个Excel文件并导出

    将List集合的数据写到一个Excel文件并导出示例: using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using System;using Sys ...

  3. springMVC(4)---生成excel文件并导出

    springMVC(4)---生成excel文件并导出 在开发过程中,需要将数据库中的数据以excel表格的方式导出. 首先说明.我这里用的是Apache的POI项目,它是目前比较成熟的HSSF接口, ...

  4. NodeJs之EXCEL文件导入导出MongoDB数据库数据

    NodeJs之EXCEL文件导入导出MongoDB数据库数据 一,介绍与需求 1.1,介绍 (1),node-xlsx : 基于Node.js解析excel文件数据及生成excel文件. (2),ex ...

  5. Excel文件导入导出(基于Nodejs、exceljs)

    Excel导入.导出是大多数项目的管理后台必备功能.几年来使用过多个该功能的实现包,最近一次开发该功能,突然发现一个人气极高(3000+)的包,这里记录一下使用方法. 大凡厉害的技术的文档咋一看都想字 ...

  6. Excel导入导出工具(简单、好用且轻量级的海量Excel文件导入导出解决方案.)

    Excel导入导出工具(简单.好用且轻量级的海量Excel文件导入导出解决方案.) 置顶 2019-09-07 16:47:10 $9420 阅读数 261更多 分类专栏: java   版权声明:本 ...

  7. 用NODEJS处理EXCEL文件导入导出,文件上传

    參考文章 http://librajt.github.io/2013/08/04/handle-excel-file-with-nodejs/ 对照了 ExcelJS ,https://github. ...

  8. 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常

    导出到Excel方法: <span style="color: rgb(0, 0, 255);">public</span> <span style= ...

  9. Excel文件导入导出

    /**     * 导入Excel文件数据     *      * @param file 将要导入的Excel文件     * @param fileCheckKeyWord 用于判断导入文件是否 ...

随机推荐

  1. nginx 搭配 lua

    据说lua的效率高,公司要求,路过学习下.哎 安装 需要最新版的Nginx,LuaJIT,ngx_devel_kit,ngx_lua等安装文件. 安装Lua或者LuaJIT都是可以的,但是出于效率的考 ...

  2. IE中JavaScript 的异常处理

    错误类型 编程中我们会遇到四种错误:编译错误.逻辑错误.输入/验证错误.运行时错误,但是在JavaScript中我们只能最后两种错误. JavaScript 1.5定义了六种错误类型: EvalErr ...

  3. 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 ...

  4. C#类和成员的修饰符

    C#中public.private.protected.internal.protected internal & (2010-09-22 13:33:45)转载 标签: 杂谈 分类: C# ...

  5. express+nodecoffee写passport登录验证实例(一)

    项目中要用到passport登录验证,环境如标题样:express框架,coffee模版引擎,node后台 一:建项目 直接用express命令建,虽然默认模版为jade,可以手动换成coffee哦. ...

  6. BZOJ1576 [Usaco2009 Jan]安全路经Travel

    首先用Dijkstra做出最短路生成树,设dis[p]为1到p点的最短路长度 对于一条不在生成树上的边u -> v,不妨设fa为u.v的lca 则一fa到v的路径上的任意点x都可以由u达到,走的 ...

  7. Singleton、MultiThread、Lib——实现单实例无锁多线程安全API

        前阵子写静态lib导出单实例多线程安全API时,出现了CRITICAL_SECTION初始化太晚的问题,之后查看了错误的资料,引导向了错误的理解,以至于今天凌晨看到另一份代码,也不多想的以为s ...

  8. "数学口袋精灵"bug的发现

    团队成员的博客园地址: 曾治业:http://www.cnblogs.com/zzy999/ 陈焕恳:http://www.cnblogs.com/4249ken/ 蓝叶:http://www.cnb ...

  9. LightOJ 1141 Program E

    Description In this problem, you are given an integer number s. You can transform any integer number ...

  10. java udp网络编程

    import java.net.*; /* 通过UDP传输发送文字数据 1.建立socket服务 2.提供数据,并封装到数据包中 3.通过sokect服务的发送功能,将数据包发送出去 4.关闭资源 * ...