Excel 파일 내보내기 작업

18989 단어 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;

        }

    }

}

좋은 웹페이지 즐겨찾기