427 lines
17 KiB
C#
427 lines
17 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using NPOI.HSSF.UserModel;
|
||
using NPOI.SS.UserModel;
|
||
using NPOI.POIFS.FileSystem;
|
||
using MyCode.Project.Infrastructure.Common;
|
||
using NPOI.SS.Util;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Data;
|
||
using NPOI.XSSF.UserModel;
|
||
using MyCode.Project.Infrastructure.Enumeration;
|
||
|
||
namespace MyCode.Project.Infrastructure.Exports
|
||
{
|
||
/// <summary>
|
||
/// 导出Excel配置类
|
||
/// </summary>
|
||
public class ExportExcelSpecial
|
||
{
|
||
|
||
#region GetDownloadPath(获取下载路径)
|
||
/// <summary>
|
||
/// 获取下载路径
|
||
/// </summary>
|
||
/// <param name="fileName">文件名</param>
|
||
/// <returns></returns>
|
||
public string GetDownloadPath(string fileName)
|
||
{
|
||
string dirPath = "/download/exceltemp/";
|
||
string absFilePath = FileUtils.GetPhysicalPath(dirPath);
|
||
if (!Directory.Exists(absFilePath))
|
||
{
|
||
Directory.CreateDirectory(absFilePath);
|
||
}
|
||
//string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_fff");
|
||
//string filePath = Path.Combine(dirPath, fileName + "_" + newFileName + ".xls");
|
||
//return filePath;
|
||
return Path.Combine(dirPath, fileName);
|
||
}
|
||
#endregion
|
||
|
||
#region NPIO 把DataTable导出到EXCEL的方法
|
||
/// <summary>
|
||
/// DataTable导出到Excel文件
|
||
/// </summary>
|
||
/// <param name="dtSource">源DataTable</param>
|
||
/// <param name="strHeaderText">表头文本</param>
|
||
/// <param name="headers">需要导出的列的列头</param>
|
||
/// <param name="cellKes">需要导出的对应的列字段</param>
|
||
/// <param name="strFileName">保存位置</param>
|
||
public void Export(DataTable dtSource, string strHeaderText, string[] headers, string[] cellKes, string strFileName)
|
||
{
|
||
// 将需要导出的数据导到excel中并生成文件流
|
||
using (MemoryStream ms = Export(dtSource, strHeaderText, headers, cellKes))
|
||
{
|
||
// 将文件流写入文件
|
||
using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
|
||
{
|
||
byte[] data = ms.ToArray();
|
||
fs.Write(data, 0, data.Length);
|
||
fs.Flush();
|
||
fs.Close();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// DataTable导出到Excel的MemoryStream
|
||
/// </summary>
|
||
/// <param name="dtSource">源DataTable</param>
|
||
/// <param name="strHeaderText">表头文本</param>
|
||
/// <param name="headers">需要导出的列的列头</param>
|
||
/// <param name="cellKes">需要导出的对应的列字段</param>
|
||
/// <returns></returns>
|
||
private MemoryStream Export(DataTable dtSource, string strHeaderText, string[] headers, string[] cellKes)
|
||
{
|
||
// excel
|
||
//HSSFWorkbook workbook = new HSSFWorkbook();
|
||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||
// 创建一个sheet页,已strHeaderText命名
|
||
ISheet sheet = workbook.CreateSheet(strHeaderText);
|
||
|
||
//#region 右击文件 属性信息
|
||
//{
|
||
// DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
|
||
// dsi.Company = "NPOI";
|
||
// workbook.DocumentSummaryInformation = dsi;
|
||
|
||
// SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
|
||
// //si.Author = "文件作者信息"; //填加xls文件作者信息
|
||
// //si.ApplicationName = "创建程序信息"; //填加xls文件创建程序信息
|
||
// //si.LastAuthor = "最后保存者信息"; //填加xls文件最后保存者信息
|
||
// //si.Comments = "作者信息"; //填加xls文件作者信息
|
||
// //si.Title = "标题信息"; //填加xls文件标题信息
|
||
// //si.Subject = "主题信息";//填加文件主题信息
|
||
// si.CreateDateTime = DateTime.Now;
|
||
// workbook.SummaryInformation = si;
|
||
//}
|
||
//#endregion
|
||
|
||
// 日期的样式
|
||
ICellStyle dateStyle = workbook.CreateCellStyle();
|
||
// 日期的格式化
|
||
IDataFormat format = workbook.CreateDataFormat();
|
||
dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");
|
||
// 字体样式
|
||
IFont datafont = workbook.CreateFont();
|
||
datafont.FontName = "微软雅黑";
|
||
// 字体大小
|
||
datafont.FontHeightInPoints = 11;
|
||
dateStyle.SetFont(datafont);
|
||
// 边框
|
||
dateStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
dateStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
dateStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
dateStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
|
||
// 其他数据的样式
|
||
ICellStyle cellStyle = workbook.CreateCellStyle();
|
||
// 字体样式
|
||
IFont cellfont = workbook.CreateFont();
|
||
// 字体大小
|
||
cellfont.FontHeightInPoints = 11;
|
||
cellfont.FontName = "宋体";
|
||
cellStyle.SetFont(cellfont);
|
||
// 边框
|
||
cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
|
||
// 总的列数
|
||
int colNum = headers.Length;
|
||
// 每个列的宽度
|
||
int[] arrColWidth = new int[colNum];
|
||
// 初始化列的宽度为列头的长度,已需要显示的列头的名字长度计算
|
||
for (int i = 0; i < headers.Length; i++)
|
||
{
|
||
arrColWidth[i] = Encoding.GetEncoding(936).GetBytes(headers[i]).Length * 2;
|
||
}
|
||
|
||
// 循环数据,取每列数据最宽的作为该列的宽度
|
||
for (int i = 0; i < dtSource.Rows.Count; i++)
|
||
{
|
||
for (int j = 0; j < cellKes.Length; j++)
|
||
{
|
||
int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][cellKes[j]].ToString()).Length;
|
||
if (intTemp > arrColWidth[j])
|
||
{
|
||
arrColWidth[j] = intTemp;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 记录生成的行数
|
||
int rowIndex = 0;
|
||
|
||
// DataTable中的列信息
|
||
DataColumnCollection columns = dtSource.Columns;
|
||
// 循环所有的行,向sheet页中添加数据
|
||
foreach (DataRow row in dtSource.Rows)
|
||
{
|
||
#region 新建表,填充表头,填充列头,样式
|
||
if (rowIndex == 65535 || rowIndex == 0)
|
||
{
|
||
// 如果不是第一行,则创建一个新的sheet页
|
||
if (rowIndex != 0)
|
||
{
|
||
sheet = workbook.CreateSheet();
|
||
}
|
||
|
||
#region 表头及样式
|
||
{
|
||
// // 在当前sheet页上创建第一行
|
||
// IRow headerRow = sheet.CreateRow(0);
|
||
// // 该行的高度
|
||
// headerRow.HeightInPoints = 50;
|
||
|
||
// // 设置第一列的值
|
||
// headerRow.CreateCell(0).SetCellValue(strHeaderText);
|
||
|
||
// // 设置列的样式
|
||
// ICellStyle headStyle = workbook.CreateCellStyle();
|
||
// // 内容居中显示
|
||
// headStyle.Alignment = HorizontalAlignment.Center;
|
||
// // 字体样式
|
||
// IFont font = workbook.CreateFont();
|
||
// font.FontName = "宋体";
|
||
// // 字体大小
|
||
// font.FontHeightInPoints = 20;
|
||
// // 粗体显示
|
||
// font.Boldweight = 700;
|
||
// // 字体颜色
|
||
// font.Color = NPOI.HSSF.Util.HSSFColor.Blue.Index;
|
||
|
||
// headStyle.SetFont(font);
|
||
// // 边框
|
||
// headStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
// headStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
// headStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
// headStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
|
||
// // 设置单元格的样式
|
||
// headerRow.GetCell(0).CellStyle = headStyle;
|
||
// headerRow.GetCell(0).CellStyle.SetFont(font);
|
||
// // 设置该行每个单元格的样式
|
||
// for (int i = 1; i < colNum; i++)
|
||
// {
|
||
// headerRow.CreateCell(i);
|
||
// headerRow.GetCell(i).CellStyle = headStyle;
|
||
// headerRow.GetCell(i).CellStyle.SetFont(font);
|
||
// }
|
||
|
||
// // 合并单元格
|
||
// sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, colNum - 1));
|
||
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region 列头及样式
|
||
{
|
||
// 创建第二行
|
||
IRow headerRow = sheet.CreateRow(0);
|
||
// 该行的高度
|
||
headerRow.HeightInPoints = 28;
|
||
// 列的样式
|
||
ICellStyle headStyle = workbook.CreateCellStyle();
|
||
// 单元格内容居中显示
|
||
headStyle.Alignment = HorizontalAlignment.Center;
|
||
// 字体样式
|
||
IFont font = workbook.CreateFont();
|
||
font.FontName = "宋体";
|
||
|
||
// 字体大小
|
||
font.FontHeightInPoints = 11;
|
||
// 粗体
|
||
font.Boldweight = 700;
|
||
// 字体颜色
|
||
font.Color = NPOI.HSSF.Util.HSSFColor.Black.Index;
|
||
headStyle.SetFont(font);
|
||
// 边框
|
||
headStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
headStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
headStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
headStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
|
||
// 设置每列的样式和值
|
||
for (int i = 0; i < headers.Length; i++)
|
||
{
|
||
headerRow.CreateCell(i).SetCellValue(headers[i]);
|
||
headerRow.GetCell(i).CellStyle = headStyle;
|
||
headerRow.GetCell(i).CellStyle.SetFont(font);
|
||
|
||
//设置列宽
|
||
if (arrColWidth[i] > 255)
|
||
{
|
||
arrColWidth[i] = 254;
|
||
}
|
||
else
|
||
{
|
||
sheet.SetColumnWidth(i, (arrColWidth[i] + 1) * 256);
|
||
}
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
rowIndex = 1;
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region 填充内容
|
||
// 创建新的一行
|
||
IRow dataRow = sheet.CreateRow(rowIndex);
|
||
// 该行的高度
|
||
dataRow.HeightInPoints = 23;
|
||
|
||
// 循环需要写入的每列数据
|
||
for (int i = 0; i < cellKes.Length; i++)
|
||
{
|
||
// 创建列
|
||
ICell newCell = dataRow.CreateCell(i);
|
||
// 获取DataTable中该列对象
|
||
DataColumn column = columns[cellKes[i]];
|
||
// 该列的值
|
||
string drValue = row[column].ToString();
|
||
|
||
// 根据值得类型分别处理之后赋值
|
||
switch (column.DataType.ToString())
|
||
{
|
||
case "System.String"://字符串类型
|
||
newCell.SetCellValue(drValue);
|
||
|
||
newCell.CellStyle = cellStyle;
|
||
break;
|
||
case "System.DateTime"://日期类型
|
||
DateTime dateV;
|
||
DateTime.TryParse(drValue, out dateV);
|
||
newCell.SetCellValue(dateV);
|
||
|
||
newCell.CellStyle = dateStyle;//格式化显示
|
||
break;
|
||
case "System.Boolean"://布尔型
|
||
bool boolV = false;
|
||
bool.TryParse(drValue, out boolV);
|
||
newCell.SetCellValue(boolV);
|
||
|
||
newCell.CellStyle = cellStyle;
|
||
break;
|
||
case "System.Int16"://整型
|
||
case "System.Int32":
|
||
case "System.Int64":
|
||
case "System.Byte":
|
||
int intV = 0;
|
||
int.TryParse(drValue, out intV);
|
||
newCell.SetCellValue(intV);
|
||
|
||
newCell.CellStyle = cellStyle;
|
||
break;
|
||
case "System.Decimal"://浮点型
|
||
case "System.Double":
|
||
double doubV = 0;
|
||
double.TryParse(drValue, out doubV);
|
||
newCell.SetCellValue(doubV);
|
||
|
||
newCell.CellStyle = cellStyle;
|
||
break;
|
||
case "System.DBNull"://空值处理
|
||
newCell.SetCellValue("");
|
||
|
||
newCell.CellStyle = cellStyle;
|
||
break;
|
||
default:
|
||
newCell.SetCellValue("");
|
||
|
||
newCell.CellStyle = cellStyle;
|
||
break;
|
||
}
|
||
|
||
newCell.CellStyle.SetFont(cellfont);
|
||
}
|
||
#endregion
|
||
|
||
rowIndex++;
|
||
}
|
||
|
||
MemoryStream ms = new MemoryStream();
|
||
workbook.Write(ms);
|
||
// ms.Flush();
|
||
// ms.Position = 0;
|
||
return ms;
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region 返回下载路径
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="reportType"></param>
|
||
/// <param name="dt"></param>
|
||
/// <returns></returns>
|
||
public string GetFilePath(ReportType reportType, DataTable dt)
|
||
{
|
||
List<string> arry = new List<string>();
|
||
foreach (DataColumn Columns in dt.Columns)
|
||
{
|
||
arry.Add(Columns.ColumnName);
|
||
}
|
||
var name = EnumHelper.GetDescription(reportType);
|
||
|
||
//execl特殊字符处理
|
||
name = RegexHelper.ToExcelSheetName(name);
|
||
|
||
string fileName = name + "_" + DateTime.Now.ToString("yyyyMMddHHmmss_fff") + ".xls";
|
||
|
||
var filePath = GetDownloadPath(fileName);
|
||
|
||
string absFilePath = FileUtils.GetPhysicalPath(filePath);
|
||
|
||
Export(dt, name, arry.ToArray(), arry.ToArray(), absFilePath);
|
||
|
||
return filePath;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 返回下载路径
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="reportType"></param>
|
||
/// <param name="dt"></param>
|
||
/// <param name="name"></param>
|
||
/// <returns></returns>
|
||
public string GetFilePathCustomName(ReportType reportType, DataTable dt, string name="")
|
||
{
|
||
List<string> arry = new List<string>();
|
||
foreach (DataColumn Columns in dt.Columns)
|
||
{
|
||
arry.Add(Columns.ColumnName);
|
||
}
|
||
if (string.IsNullOrWhiteSpace(name)) { name = EnumHelper.GetDescription(reportType); }
|
||
|
||
//execl特殊字符处理
|
||
name = RegexHelper.ToExcelSheetName(name);
|
||
|
||
string fileName = name + "_" + DateTime.Now.ToString("yyyyMMddHHmmss_fff") + ".xls";
|
||
|
||
var filePath = GetDownloadPath(fileName);
|
||
|
||
string absFilePath = FileUtils.GetPhysicalPath(filePath);
|
||
|
||
Export(dt, name, arry.ToArray(), arry.ToArray(), absFilePath);
|
||
|
||
return filePath;
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|