@ -1,4 +1,11 @@
using System ;
using NPOI.HSSF.UserModel ;
using NPOI.OpenXmlFormats.Spreadsheet ;
using NPOI.SS.UserModel ;
using NPOI.SS.Util ;
using NPOI.XSSF.UserModel ;
using SqlSugar ;
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.ComponentModel.DataAnnotations ;
@ -7,12 +14,6 @@ using System.IO;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using NPOI.HSSF.UserModel ;
using NPOI.OpenXmlFormats.Spreadsheet ;
using NPOI.SS.UserModel ;
using NPOI.SS.Util ;
using NPOI.XSSF.UserModel ;
using SqlSugar ;
using Tiobon.Core.Common ;
using Tiobon.Core.Common.Extensions ;
using Tiobon.Core.Model.Models ;
@ -107,6 +108,296 @@ public static class ReportHelper
public static async Task < string > ImportTemplate ( ISqlSugarClient Db , List < QueryExportColumn > tableColumn , DataTable dt , string menuName )
{
//生成表格
try
{
//生成文件至文件服务器
var fid = SnowFlakeSingle . Instance . NextId ( ) ;
var filepath = $"{$" { Environment . CurrentDirectory } { Path . DirectorySeparatorChar } wwwroot { Path . DirectorySeparatorChar } files { Path . DirectorySeparatorChar } import { Path . DirectorySeparatorChar } { fid } { Path . DirectorySeparatorChar } "}" ;
if ( ! Directory . Exists ( filepath ) )
Directory . CreateDirectory ( filepath ) ;
var fname = $"{menuName}.xlsx" ;
var sheetName = "导入数据" ;
IWorkbook workbook = new XSSFWorkbook ( ) ;
ISheet sheet = workbook . CreateSheet ( sheetName ) ;
ICellStyle dateStyle = workbook . CreateCellStyle ( ) ;
IDataFormat format = workbook . CreateDataFormat ( ) ;
dateStyle . DataFormat = format . GetFormat ( "yyyy-mm-dd" ) ;
ICellStyle datetimeStyle = workbook . CreateCellStyle ( ) ;
datetimeStyle . DataFormat = format . GetFormat ( "yyyy-mm-dd hh:mm" ) ;
ICellStyle datetimesStyle = workbook . CreateCellStyle ( ) ;
datetimesStyle . DataFormat = format . GetFormat ( "yyyy-mm-dd hh:mm:ss" ) ;
int [ ] arrColWidth = new int [ tableColumn . Count ] ;
for ( int i = 0 ; i < tableColumn . Count ; i + + )
{
arrColWidth [ i ] = Encoding . GetEncoding ( "utf-8" ) . GetBytes ( tableColumn [ i ] . label ) . Length ;
}
for ( int i = 0 ; i < 1 ; i + + )
{
for ( int j = 0 ; j < tableColumn . Count ; j + + )
{
int intTemp = Encoding . GetEncoding ( "utf-8" ) . GetBytes ( tableColumn [ j ] . label ) . Length ;
if ( intTemp > arrColWidth [ j ] )
{
arrColWidth [ j ] = intTemp ;
}
}
}
int rowIndex = 0 ;
int intTop = 0 ;
int HeightInPoints = 4 0 ;
int listColIndex = 0 ;
//foreach (DataRow row1 in dt.Rows)
//{
//}
#region 新建表、填充表头、填充列头,样式
if ( rowIndex = = 6 5 5 3 5 0 | | rowIndex = = 0 )
{
if ( rowIndex ! = 0 )
{
sheet = workbook . CreateSheet ( ) ;
}
intTop = 0 ;
#region 列头及样式
{
IRow headerRow = sheet . CreateRow ( intTop ) ;
headerRow . HeightInPoints = HeightInPoints ;
intTop + = 1 ;
ICellStyle headStyle = workbook . CreateCellStyle ( ) ;
headStyle . Alignment = HorizontalAlignment . Center ; //居中
headStyle . VerticalAlignment = VerticalAlignment . Center ; //垂直居中
headStyle . WrapText = true ; //自动换行
// 边框
headStyle . BorderBottom = BorderStyle . Thin ;
headStyle . BorderLeft = BorderStyle . Thin ;
headStyle . BorderRight = BorderStyle . Thin ;
headStyle . BorderTop = BorderStyle . Thin ;
ICellStyle headRequiredStyle = workbook . CreateCellStyle ( ) ;
headRequiredStyle . Alignment = HorizontalAlignment . Center ; //居中
headRequiredStyle . VerticalAlignment = VerticalAlignment . Center ; //垂直居中
headRequiredStyle . WrapText = true ; //自动换行
// 边框
headRequiredStyle . BorderBottom = BorderStyle . Thin ;
headRequiredStyle . BorderLeft = BorderStyle . Thin ;
headRequiredStyle . BorderRight = BorderStyle . Thin ;
headRequiredStyle . BorderTop = BorderStyle . Thin ;
// 字体
IFont font = workbook . CreateFont ( ) ;
font . FontHeightInPoints = ( short ) 1 0 ;
font . IsBold = true ;
font . FontName = "宋体" ;
headStyle . SetFont ( font ) ;
IFont font2 = workbook . CreateFont ( ) ;
font2 . FontHeightInPoints = ( short ) 1 0 ;
font2 . IsBold = true ;
font2 . FontName = "宋体" ;
font2 . Color = IndexedColors . Red . Index ;
headRequiredStyle . SetFont ( font2 ) ;
//ICellStyle headStyle = workbook.CreateCellStyle();
//headStyle.Alignment = HorizontalAlignment.Center;
//headStyle.BorderBottom = BorderStyle.Medium;
//headStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index;
//headStyle.FillPattern = FillPattern.NoFill;
//IFont font = workbook.CreateFont();
//font.Boldweight = 700;
//headStyle.SetFont(font);
for ( int j = 0 ; j < tableColumn . Count ; j + + )
{
string dataSource = tableColumn [ j ] . dataSource ;
string field = tableColumn [ j ] . field ;
string label = tableColumn [ j ] . label ;
headerRow . CreateCell ( j ) . SetCellValue ( label ) ;
if ( tableColumn [ j ] . required = = "true" )
headerRow . GetCell ( j ) . CellStyle = headRequiredStyle ;
else
headerRow . GetCell ( j ) . CellStyle = headStyle ;
//设置列宽
//sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);
if ( arrColWidth [ j ] > 2 5 5 )
{
arrColWidth [ j ] = 2 5 4 ;
}
else
{
sheet . SetColumnWidth ( j , ( arrColWidth [ j ] + 1 ) * 2 5 6 ) ;
}
//是否下拉
if ( ! string . IsNullOrEmpty ( dataSource )
& & ! dataSource . StartsWith ( "OrgTreeWith" )
& & ! dataSource . StartsWith ( "StaffWith" )
)
{
var dataSourceLists = GetDataSourceLists ( Db , field , dataSource ) ;
var fields = new List < string > ( )
{
"InOrOut" ,
"ResumeType" ,
"ImportanceDegree" ,
"YearHumanId" ,
"TitleId"
} ;
try
{
if ( dataSourceLists . Any ( ) & & fields . Contains ( field ) )
{
//设置数据源的值
IDataValidationConstraint dvConstraint1 = sheet . GetDataValidationHelper ( ) . CreateExplicitListConstraint ( dataSourceLists . Where ( x = > x . name . IsNotEmptyOrNull ( ) ) . Select ( x = > x . name ) . ToArray ( ) ) ;
//设置编辑的区域
IDataValidation dataValidation1 = sheet . GetDataValidationHelper ( ) . CreateValidation ( dvConstraint1 , new CellRangeAddressList ( 1 , 6 5 5 3 5 , j , j ) ) ;
sheet . AddValidationData ( dataValidation1 ) ;
}
}
catch ( Exception E )
{
}
// 多选 ,自行输入不报错
//if (drow.elementType.ToString().LastIndexOf("multiple") == -1)
//{
// dataValidate.CreateErrorBox("输入不合法", "请输入或选择下拉列表中的值。");
// dataValidate.ShowPromptBox = true;
//}
//else
//{
// dataValidate.ShowErrorBox = false;
//}
}
}
}
# endregion
rowIndex = intTop ;
}
# endregion
#region 填充内容
IRow dataRow = sheet . CreateRow ( rowIndex ) ;
dataRow . HeightInPoints = 2 5 ;
ICellStyle style = workbook . CreateCellStyle ( ) ;
style . Alignment = HorizontalAlignment . Center ; //居中
style . VerticalAlignment = VerticalAlignment . Center ; //垂直居中
style . WrapText = true ; //自动换行
// 边框
style . BorderBottom = BorderStyle . Thin ;
style . BorderLeft = BorderStyle . Thin ;
style . BorderRight = BorderStyle . Thin ;
style . BorderTop = BorderStyle . Thin ;
// 字体
var font1 = workbook . CreateFont ( ) ;
font1 . FontHeightInPoints = ( short ) 1 0 ;
font1 . FontName = "宋体" ;
style . SetFont ( font1 ) ;
# endregion
for ( int j = 0 ; j < tableColumn . Count ; j + + )
{
//设置列宽
//sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);
if ( arrColWidth [ j ] > 2 5 5 )
{
arrColWidth [ j ] = 2 5 4 ;
}
else
{
sheet . SetColumnWidth ( j , ( arrColWidth [ j ] + 1 ) * 2 5 6 ) ;
}
}
//rowIndex++;
for ( int columnNum = 0 ; columnNum < = tableColumn . Count ; columnNum + + )
{
sheet . AutoSizeColumn ( columnNum ) ; //先来个常规自适应
var columnWidth = sheet . GetColumnWidth ( columnNum ) / 2 5 6 ;
for ( int rowNum = 1 ; rowNum < = sheet . LastRowNum ; rowNum + + )
{
IRow currentRow ;
//当前行未被使用过
if ( sheet . GetRow ( rowNum ) = = null )
{
currentRow = sheet . CreateRow ( rowNum ) ;
}
else
{
currentRow = sheet . GetRow ( rowNum ) ;
}
if ( currentRow . GetCell ( columnNum ) ! = null )
{
ICell currentCell = currentRow . GetCell ( columnNum ) ;
int length = Encoding . Default . GetBytes ( currentCell . ToString ( ) ) . Length ;
if ( columnWidth < length )
{
columnWidth = length ;
if ( columnWidth > 3 0 ) columnWidth = 3 0 ;
}
}
}
try
{
sheet . SetColumnWidth ( columnNum , columnWidth * 3 0 0 ) ; // 256
}
catch ( Exception e )
{
}
}
for ( int rowNum = 2 ; rowNum < = sheet . LastRowNum ; rowNum + + )
{
IRow currentRow = sheet . GetRow ( rowNum ) ;
int length = 2 5 ;
foreach ( var item in currentRow . Cells )
{
if ( item ! = null )
length = Encoding . UTF8 . GetBytes ( item . ToString ( ) ) . Length > length ? Encoding . UTF8 . GetBytes ( item . ToString ( ) ) . Length : length ;
}
currentRow . HeightInPoints = 3 5 * ( length / 1 5 0 + 1 ) ;
}
//转为字节数组
MemoryStream stream = new MemoryStream ( ) ;
workbook . Write ( stream ) ;
var buf = stream . ToArray ( ) ;
//保存为Excel文件
using ( FileStream fs = new FileStream ( filepath + fname , FileMode . Create , FileAccess . Write ) )
{
fs . Write ( buf , 0 , buf . Length ) ;
fs . Flush ( ) ;
}
return filepath + fname ;
}
catch ( Exception ex )
{
}
return default ( string ) ;
}
public static async Task < string > ImportTemplate1 ( ISqlSugarClient Db , List < QueryExportColumn > tableColumn , DataTable dt , string menuName )
{
//生成表格
@ -140,19 +431,35 @@ public static class ReportHelper
//隐藏下拉数据Sheet
//fileWorkbook.SetSheetHidden(fileWorkbook.GetSheetIndex("下拉数据"), SheetState.Hidden);
IRow erow_1 = sheet . CreateRow ( 0 ) ; //ID行
IRow erow_2 = sheet . CreateRow ( 1 ) ; //文本行
IRow headerRow = sheet . CreateRow ( 0 ) ; //文本行
erow_1 . CreateCell ( 0 ) . SetCellValue ( "ExcelNums" ) ;
erow_2 . CreateCell ( 0 ) . SetCellValue ( "序号" ) ;
headerRow . HeightInPoints = 3 0 ;
head erR ow. CreateCell ( 0 ) . SetCellValue ( "序号" ) ;
//必填颜色
var CellRed = fileWorkbook . CreateCellStyle ( ) ; // 创建单元格样式
IFont Font = fileWorkbook . CreateFont ( ) ; // 创建字体
Font . Color = IndexedColors . Red . Index ; // 选择字体颜色
CellRed . SetFont ( Font ) ; // 把字体赋给样式
erow_2 . GetCell ( 0 ) . CellStyle = CellRed ;
//var CellRed = fileWorkbook.CreateCellStyle(); // 创建单元格样式
//IFont Font = fileWorkbook.CreateFont(); // 创建字体
//Font.Color = IndexedColors.Red.Index; // 选择字体颜色
//CellRed.SetFont(Font); // 把字体赋给样式
ICellStyle headStyle = fileWorkbook . CreateCellStyle ( ) ;
headStyle . Alignment = HorizontalAlignment . Center ; //居中
headStyle . VerticalAlignment = VerticalAlignment . Center ; //垂直居中
headStyle . WrapText = true ; //自动换行
// 边框
headStyle . BorderBottom = BorderStyle . Thin ;
headStyle . BorderLeft = BorderStyle . Thin ;
headStyle . BorderRight = BorderStyle . Thin ;
headStyle . BorderTop = BorderStyle . Thin ;
// 字体
IFont font = fileWorkbook . CreateFont ( ) ;
font . FontHeightInPoints = ( short ) 1 0 ;
font . IsBold = true ;
font . FontName = "宋体" ;
headStyle . SetFont ( font ) ;
headerRow . GetCell ( 0 ) . CellStyle = headStyle ;
//文本类型
ICellStyle cellString = fileWorkbook . CreateCellStyle ( ) ;
@ -165,10 +472,10 @@ public static class ReportHelper
foreach ( var drow in tableColumn )
{
int SortNum = Convert . ToInt32 ( drow . sortNum ) ;
ICell cell = erow_1 . CreateCell ( SortNum ) ;
ICell cel2 = erow_2 . CreateCell ( SortNum ) ;
ICell cel2 = headerRow . CreateCell ( SortNum ) ;
string field = drow . field ;
;
cel2 . CellStyle = headStyle ;
string label = drow . label ;
// 批注
string commentText = string . Empty ;
@ -179,7 +486,6 @@ public static class ReportHelper
catch ( Exception )
{
}
cell . SetCellValue ( field ) ;
cel2 . SetCellValue ( label ) ;
// 设置批注
@ -191,7 +497,7 @@ public static class ReportHelper
//是否必填
if ( drow . required = = "true" )
{
cel2 . CellStyle = CellRed ;
cel2 . CellStyle = headStyle ;
}
string dataSource = drow . dataSource ;
@ -275,21 +581,23 @@ public static class ReportHelper
ColNums = ColNums + 1 ;
}
erow_1 . HeightInPoints = 1 ;
//erow_1.HeightInPoints = 1 ;
// 如果导出模板存在默认数据
//if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
for ( int i = 0 ; i < dt . Rows . Count ; i + + )
{
IRow row1 = sheet . CreateRow ( i + 2 ) ;
ICell cell = row1 . CreateCell ( 0 ) ;
cell . SetCellValue ( i + 1 ) ;
for ( int j = 1 ; j < = tableColumn . Count ; j + + )
{
cell = row1 . CreateCell ( j ) ;
cell . SetCellValue ( dt . Rows [ i ] [ tableColumn [ j - 1 ] . field ] . ToString ( ) ) ;
}
}
//for (int i = 0; i < dt.Rows.Count; i++)
//{
// IRow row1 = sheet.CreateRow(i + 1);
// ICell cell = row1.CreateCell(0);
// cell.SetCellValue(i + 1);
// for (int j = 1; j <= tableColumn.Count; j++)
// {
// cell = row1.CreateCell(j);
// //if (dt.Rows[i][tableColumn[j - 1].field] != null)
// // cell.SetCellValue(dt.Rows[i][tableColumn[j - 1].field].ObjToString());
// }
//}
// Sheet 更改列为自适应宽度
for ( int col = 0 ; col < = ColNums ; col + + )
@ -421,7 +729,7 @@ public static class ReportHelper
//}
//else
{
string listSQL = string . Format ( @"exec[dbo].[PRI_ListValue] '{0}','GetEnableList','',1,63 ,'',9999,1,2" , dataSource ) ;
string listSQL = string . Format ( @"exec[dbo].[PRI_ListValue] '{0}','GetEnableList','',1,1 ,'',9999,1,2" , dataSource ) ;
DataSet listds = Db . Ado . GetDataSetAll ( listSQL ) ;
if ( listds . Tables . Count > 3 )
{
@ -449,7 +757,12 @@ public static class ReportHelper
{
}
}
dataSourceList . Add ( _d ataSource ) ;
var dataSourceStr = string . Join ( "," , dataSourceList . Select ( x = > x . name ) ) ;
if ( dataSourceList . Any ( ) & & _d ataSource . name . IsNotEmptyOrNull ( ) )
dataSourceStr = dataSourceStr + "," + _d ataSource . name ;
if ( dataSourceStr . Length < = 2 5 5 & & _d ataSource . name . IsNotEmptyOrNull ( ) )
dataSourceList . Add ( _d ataSource ) ;
}
}
}