zoukankan      html  css  js  c++  java
  • 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(0) as HSSFSheet;
                        for (int i = 0; i <= sheet.LastRowNum; i++)
                        {
                            HSSFRow row = sheet.GetRow(i) as HSSFRow;
                            ContactInfo contactInfo = new ContactInfo();
                            contactInfo.Id = Guid.NewGuid().ToString();
                            if (row.GetCell(0) != null)
                            {
                                row.GetCell(0).SetCellType(CellType.STRING);
                                contactInfo.CenterName = row.GetCell(0).StringCellValue.ToString();
                            }
                            else
                            {
                                contactInfo.CenterName = "";
                            }
                            if (row.GetCell(0) != null)
                            {
                                row.GetCell(1).SetCellType(CellType.STRING);
                                contactInfo.Name = row.GetCell(1).StringCellValue.ToString();
                            }
                            else
                            {
                                contactInfo.Name = "";
                            }
                            if (row.GetCell(2) != null)
                            {
                                row.GetCell(2).SetCellType(CellType.STRING);
                                contactInfo.PhoneNumber = row.GetCell(2).StringCellValue.ToString();
                            }
                            else
                            {
                                contactInfo.PhoneNumber = "";
                            }
                            if (row.GetCell(3) != null)
                            {
                                row.GetCell(3).SetCellType(CellType.STRING);
                                contactInfo.Address = row.GetCell(3).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(0, 24 * 256);
                sheet1.SetColumnWidth(1, 16 * 256);
                sheet1.SetColumnWidth(2, 16 * 256);
                sheet1.SetColumnWidth(3, 46 * 256);
                Row row = sheet1.CreateRow(0);
                row.HeightInPoints = 16;
                row.CreateCell(0).SetCellValue("中心名称");
                row.CreateCell(1).SetCellValue("联系人");
                row.CreateCell(2).SetCellValue("联系方式");
                row.CreateCell(3).SetCellValue("地址");
                CellStyle style = workbook.CreateCellStyle();
                style.Alignment = HorizontalAlignment.CENTER;
                style.WrapText = true;
                Font font = workbook.CreateFont();
                font.FontHeightInPoints = 12;
                font.Boldweight = (short)FontBoldWeight.BOLD;
                font.Color = (short)FontColor.RED;
                style.SetFont(font);
                for (int i = 0; i < 4; i++)
                {
                    row.GetCell(i).CellStyle = style;
                }
    
                for (int i = 1; i < list.Count; i++)
                {
                    row = sheet1.CreateRow(i);
                    row.CreateCell(0).SetCellValue(list[i - 1].CenterName);
                    row.CreateCell(1).SetCellValue(list[i - 1].Name);
                    row.CreateCell(2).SetCellValue(list[i - 1].PhoneNumber);
                    row.CreateCell(3).SetCellValue(list[i - 1].Address);
                }
                MemoryStream ms = new MemoryStream();
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                return ms;
            }
           
            //异常信息导出
            public MemoryStream ExportExcptionToExcel(string fileName, IList<ExcptionInfo> list)
            {
                HSSFWorkbook workbook = new HSSFWorkbook();
                Sheet sheet1 = workbook.CreateSheet("Sheet1");
                sheet1.SetColumnWidth(0, 12 * 256);
                sheet1.SetColumnWidth(1, 20 * 256);
                sheet1.SetColumnWidth(2, 16 * 256);
                sheet1.SetColumnWidth(3, 36 * 256);
                sheet1.SetColumnWidth(4, 16 * 256);
                sheet1.SetColumnWidth(5, 40 * 256);
                sheet1.SetColumnWidth(6, 20 * 256);
                Row row = sheet1.CreateRow(0);
                row.HeightInPoints = 16;
                row.CreateCell(0).SetCellValue("车牌号");
                row.CreateCell(1).SetCellValue("线路");
                row.CreateCell(2).SetCellValue("手机号");
                row.CreateCell(3).SetCellValue("异常情况");
                row.CreateCell(4).SetCellValue("异常类型");
                row.CreateCell(5).SetCellValue("位置");
                row.CreateCell(6).SetCellValue("上报时间");
                CellStyle style = workbook.CreateCellStyle();
                style.Alignment = HorizontalAlignment.CENTER;
                style.WrapText = true;
                Font font = workbook.CreateFont();
                font.FontHeightInPoints = 12;
                font.Boldweight = (short)FontBoldWeight.BOLD;
                font.Color = (short)FontColor.RED;
                style.SetFont(font);
                for (int i = 0; i < 7; i++)
                {
                    row.GetCell(i).CellStyle = style;
                }
                for (int i = 1; i < list.Count; i++)
                {
                    row = sheet1.CreateRow(i);
                    row.CreateCell(0).SetCellValue(list[i].LiencePlateNumber);
                    row.CreateCell(1).SetCellValue(list[i].CarLine);
                    row.CreateCell(2).SetCellValue(list[i].PhoneNumber);
                    row.CreateCell(3).SetCellValue(list[i].Remark);
                    row.CreateCell(4).SetCellValue(list[i].ExcptionCategory);
                    row.CreateCell(5).SetCellValue(list[i].Position);
                    row.CreateCell(6).SetCellValue(list[i].CreateTime.ToString());
                }
                MemoryStream ms = new MemoryStream();
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                return ms;
            }
        }
    }
  • 相关阅读:
    es学习-java操作 2.4.0版本
    es学习-基础增删改查
    mongodb 查询条件
    mongodb-查询
    mysql 性能优化
    mysql 存储过程学习(总)
    MySQL 存储过程 -流程控制的使用
    MySQL 存储过程 -光标的使用
    maven的聚合和继承
    mavean的依赖传递和排除依赖
  • 原文地址:https://www.cnblogs.com/slu182/p/4252711.html
Copyright © 2011-2022 走看看