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;
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Excel Grep toolExcel Grep tool ■히나가타 ■ 시트 구성 ExcelGrep.cls...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.