2025-01-13 16:12:34 +08:00
using BLL ;
using NPOI.SS.UserModel ;
using NPOI.SS.Util ;
using NPOI.XSSF.UserModel ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.SqlClient ;
using System.IO ;
using System.Linq ;
using System.Web ;
using System.Web.UI ;
namespace FineUIPro.Web.ContinuousPrint
{
public partial class HardnessCheckOrderPrint : PageBase
{
protected void Page_Load ( object sender , EventArgs e )
{
if ( ! IsPostBack )
{
BindGrid ( ) ;
}
}
#region 绑 定 Grid1
/// <summary>
/// 绑定Grid1
/// </summary>
public void BindGrid ( )
{
var project = BLL . Base_ProjectService . GetOnProjectListByUserId ( this . CurrUser . UserId , "1" ) ;
if ( ! string . IsNullOrEmpty ( txtProjectCode . Text ) )
{
project = project . Where ( e = > e . ProjectCode . Contains ( txtProjectCode . Text . Trim ( ) ) ) . ToList ( ) ;
}
Grid1 . RecordCount = project . Count ;
var table = this . GetPagedDataTable ( Grid1 , project ) ;
Grid1 . DataSource = table ;
Grid1 . DataBind ( ) ;
}
#endregion
#region 绑 定 Grid2
/// <summary>
/// 绑定Grid2
/// </summary>
public void BindGrid1 ( string ProjectId )
{
var str = string . Empty ;
//是否打印
if ( ! string . IsNullOrEmpty ( this . drpIsPrint . SelectedValue ) )
{
str + = " and a.IsPrintTrust=" + this . drpIsPrint . SelectedValue + "" ;
}
string strSql = @"select a.HardTestReportId,a.HardTestReportCode,a.ProjectId,b.ProjectName,a.ISO_ID,a.TrustUnitId,a.TestDate,a.InstrumentType,a.HotProessState,a.SurfaceAppearance,a.TestMethod,a.ContractUnit,a.IsPrintReport,(case a.IsPrintReport when 1 then '已打印' else '未打印' end) as IsPrintReportName,(case a.IsPrintTrust when 1 then '已打印' else '未打印' end) as IsPrintTrustName from HJGL_CH_HardTestReport as a inner join Base_Project as b on a.ProjectId=b.ProjectId where a.ProjectId=@ProjectId" + str ;
List < SqlParameter > listStr = new List < SqlParameter > ( ) ;
listStr . Add ( new SqlParameter ( "@ProjectId" , ProjectId ) ) ;
SqlParameter [ ] parameter = listStr . ToArray ( ) ;
Grid2 . DataSource = SQLHelper . GetDataTableRunText ( strSql , parameter ) ;
Grid2 . DataBind ( ) ;
Grid2 . SelectAllRows ( ) ;
}
#endregion
#region 分 页 、 排 序
/// <summary>
/// 分页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_PageIndexChange ( object sender , GridPageEventArgs e )
{
Grid1 . PageIndex = e . NewPageIndex ;
BindGrid ( ) ;
}
/// <summary>
/// 分页显示条数下拉框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged ( object sender , EventArgs e )
{
Grid1 . PageSize = Convert . ToInt32 ( ddlPageSize . SelectedValue ) ;
BindGrid ( ) ;
}
/// <summary>
/// 排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_Sort ( object sender , FineUIPro . GridSortEventArgs e )
{
Grid1 . SortDirection = e . SortDirection ;
Grid1 . SortField = e . SortField ;
BindGrid ( ) ;
}
#endregion
#region 选 择 加 载
/// <summary>
/// 选择加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_RowClick ( object sender , GridRowClickEventArgs e )
{
BindGrid1 ( this . Grid1 . SelectedRowID ) ;
}
#endregion
#region 私 有 方 法
/// <summary>
/// 得到热处理类型
/// </summary>
/// <returns></returns>
protected string ConvertProessTypes ( object ProessTypes )
{
string proessTypes = string . Empty ;
if ( ProessTypes ! = null )
{
proessTypes = BLL . HJGL_PW_JointInfoService . ConvertProessTypes ( ProessTypes . ToString ( ) ) ;
}
return proessTypes ;
}
#endregion
#region 关 闭 弹 出 窗 口
/// <summary>
/// 关闭窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Window1_Close ( object sender , EventArgs e )
{
BindGrid ( ) ;
}
#endregion
#region 查 询
/// <summary>
/// 查询
/// </summary>
protected void btnSubmit_Click ( object sender , EventArgs e )
{
BindGrid ( ) ;
}
/// <summary>
/// 是否打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void drpIsPrint_SelectedIndexChanged ( object sender , EventArgs e )
{
BindGrid1 ( this . Grid1 . SelectedRowID ) ;
}
#endregion
#region 打 印 焊 接 记 录 打 印
/// <summary>
/// 打印焊接记录打印
/// </summary>
protected void btnPrint_Click ( object sender , EventArgs e )
{
if ( Grid2 . SelectedRowIndexArray . Length < = 0 )
{
Alert . ShowInTop ( "最少选中一行!" , MessageBoxIcon . Warning ) ;
return ;
}
string rootPath = Server . MapPath ( "~/" ) + Const . ExcelUrl ;
//导出文件
string filePath = rootPath + DateTime . Now . ToString ( "yyyyMMddhhmmss" ) + "\\" ;
if ( ! Directory . Exists ( filePath ) )
{
Directory . CreateDirectory ( filePath ) ;
}
string ReportFileName = filePath + "out.xlsx" ;
if ( Grid2 . Rows . Count > 0 )
{
int rowIndex = 0 ;
XSSFWorkbook hssfworkbook = new XSSFWorkbook ( ) ;
XSSFSheet ws = ( XSSFSheet ) hssfworkbook . CreateSheet ( "硬度检测委托单" ) ;
#region 列 宽
ws . SetColumnWidth ( 0 , ( 9 * 256 ) - 95 ) ; //(8.08)8.08
ws . SetColumnWidth ( 1 , ( 9 * 256 ) - 95 ) ; //(8.08)8.08
ws . SetColumnWidth ( 2 , ( 2 * 256 ) - 95 ) ; //(6.08)6.08
ws . SetColumnWidth ( 3 , ( 11 * 256 ) - 95 ) ; //(10.08)10.08
ws . SetColumnWidth ( 4 , ( 8 * 256 ) - 15 ) ; //(14.33)14.25
ws . SetColumnWidth ( 5 , ( 9 * 256 ) + 15 ) ; //(20.55)20.5
ws . SetColumnWidth ( 6 , ( 9 * 256 ) - 95 ) ; //(8.08)8.08
ws . SetColumnWidth ( 7 , ( 9 * 256 ) - 95 ) ; //(8.08)8.08
ws . SetColumnWidth ( 8 , ( 1 * 256 ) - 95 ) ; //(8.08)8.08
ws . SetColumnWidth ( 9 , ( 9 * 256 ) - 95 ) ; //(8.08)8.08
ws . SetColumnWidth ( 10 , ( 7 * 256 ) - 95 ) ; //(8.08)8.08
ws . SetColumnWidth ( 11 , ( 5 * 256 ) + 100 ) ; //(4.83)4.83
ws . SetColumnWidth ( 12 , ( 13 * 256 ) + 200 ) ; //(12.17)12.17
#endregion
for ( int gi = 0 ; gi < Grid2 . Rows . Count ; gi + + )
{
if ( Grid2 . SelectedRowIndexArray . Contains ( gi ) )
{
System . Web . UI . WebControls . HiddenField hidHardTestReportId = Grid2 . Rows [ gi ] . FindControl ( "hidHardTestReportId" ) as System . Web . UI . WebControls . HiddenField ;
System . Web . UI . WebControls . HiddenField hidProjectName = Grid2 . Rows [ gi ] . FindControl ( "hidProjectName" ) as System . Web . UI . WebControls . HiddenField ;
//打印赋值
Model . HJGL_CH_HardTestReport report = BLL . HJGL_CH_HardTestReportService . GetCH_HardTestReportByID ( hidHardTestReportId . Value ) ;
if ( report ! = null )
{
report . IsPrintTrust = true ;
report . PrintTrustDate = DateTime . Now ;
report . TrustName = report . HotHardCode + "-" + string . Format ( "{0:yyyy-MM-dd}" , DateTime . Now ) ;
BLL . HJGL_CH_HardTestReportService . UpdateCH_HardTestReport ( report ) ;
}
//头部
var listTitleStr = new List < SqlParameter > ( ) ;
listTitleStr . Add ( new SqlParameter ( "@HardTestReportId" , hidHardTestReportId . Value ) ) ;
SqlParameter [ ] titleparameter = listTitleStr . ToArray ( ) ;
var tbTitle = SQLHelper . GetDataTableRunProc ( "HJGL_spCH_HardTestReport" , titleparameter ) ;
//列表
var listStr = new List < SqlParameter > ( ) ;
listStr . Add ( new SqlParameter ( "@HardTestReportId" , hidHardTestReportId . Value ) ) ;
SqlParameter [ ] parameter = listStr . ToArray ( ) ;
var tb = SQLHelper . GetDataTableRunProc ( "HJGL_spCH_HardTestReportItemDistinct2" , parameter ) ;
if ( tb . Rows . Count > 0 & & tbTitle . Rows . Count > 0 )
{
CellRangeAddress region ;
//尾页面总数
var pageNum = 0d ;
//尾部增加行
var endaddNum = 0 ;
//公共样式
ICellStyle style = CommonPrint . SetExcelStyle ( hssfworkbook , BorderStyle . Thin , BorderStyle . Thin , BorderStyle . Thin , BorderStyle . Thin , VerticalAlignment . Center , HorizontalAlignment . Center , 10 , true ) ;
//文字靠左左侧无边框
ICellStyle styleFontLeft = CommonPrint . SetExcelStyle ( hssfworkbook , BorderStyle . Thin , BorderStyle . None , BorderStyle . Thin , BorderStyle . Thin , VerticalAlignment . Center , HorizontalAlignment . Left , 10 , true ) ;
//文字靠左右边无边框
ICellStyle styleRightNoneFontLeft = CommonPrint . SetExcelStyle ( hssfworkbook , BorderStyle . Thin , BorderStyle . Thin , BorderStyle . None , BorderStyle . Thin , VerticalAlignment . Center , HorizontalAlignment . Left , 10 , true ) ;
//头部样式
ICellStyle styleTou = CommonPrint . SetExcelStyle ( hssfworkbook , BorderStyle . Thin , BorderStyle . Thin , BorderStyle . Thin , BorderStyle . Thin , VerticalAlignment . Center , HorizontalAlignment . Center , 16 , true , true ) ;
//计算页
//pageNum =
//tb.Rows.Count <= 11 ? 1
//: (tb.Rows.Count > 11 && tb.Rows.Count <= 27) ? 2
//: Math.Ceiling((float)(tb.Rows.Count - 27) / 16) + 2;
//CellRangeAddress region;
pageNum = tb . Rows . Count < 27 ? 1
: Math . Ceiling ( ( float ) ( tb . Rows . Count - 27 ) / 38 ) + 1 ;
//循环页
for ( int i = 1 ; i < = pageNum ; i + + )
{
//取数据开始和结束条数
var dStart = 0 ;
var dEnd = 0 ;
//excel数据开始行和结束行
var tStart = 0 ;
var tEnd = 0 ;
#region 头 部 和 每 页 数 据 参 数
//第一页和第二页需要创建头
if ( i = = 1 )
{
//尾部增加行
//endaddNum = 8;
//创建头部行和列
ws = ExcelCreateRowTitle ( ws , hssfworkbook , rowIndex , rowIndex + 7 , style , 0 , 12 ) ;
//取数据开始和结束条数
dStart = 0 ;
dEnd = 27 ;
//excel数据开始行和结束行
tStart = rowIndex + 8 ;
tEnd = rowIndex + 35 ;
#region 头 部
//行1
region = new CellRangeAddress ( rowIndex , rowIndex + 1 , 0 , 2 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex ) . GetCell ( 0 ) . SetCellValue ( "" ) ;
region = new CellRangeAddress ( rowIndex , rowIndex + 1 , 3 , 8 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex ) . GetCell ( 3 ) . SetCellValue ( "硬度检测委托单" ) ;
ws . GetRow ( rowIndex ) . GetCell ( 3 ) . CellStyle = styleTou ;
region = new CellRangeAddress ( rowIndex , rowIndex , 9 , 10 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex ) . GetCell ( 9 ) . SetCellValue ( "工程名称:" ) ;
region = new CellRangeAddress ( rowIndex , rowIndex , 11 , 12 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex ) . GetCell ( 10 ) . CellStyle = styleRightNoneFontLeft ;
ws . GetRow ( rowIndex ) . GetCell ( 11 ) . SetCellValue ( hidProjectName . Value ) ;
ws . GetRow ( rowIndex ) . GetCell ( 11 ) . CellStyle = styleFontLeft ;
//行2
region = new CellRangeAddress ( rowIndex + 1 , rowIndex + 1 , 9 , 10 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 1 ) . GetCell ( 9 ) . SetCellValue ( "单位工程名称:" ) ;
region = new CellRangeAddress ( rowIndex + 1 , rowIndex + 1 , 11 , 12 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 1 ) . GetCell ( 10 ) . CellStyle = styleRightNoneFontLeft ;
ws . GetRow ( rowIndex + 1 ) . GetCell ( 11 ) . SetCellValue ( "管道安装工程" ) ;
ws . GetRow ( rowIndex + 1 ) . GetCell ( 11 ) . CellStyle = styleFontLeft ;
//行3
region = new CellRangeAddress ( rowIndex + 2 , rowIndex + 2 , 0 , 1 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 2 ) . GetCell ( 0 ) . SetCellValue ( "检测单位" ) ;
region = new CellRangeAddress ( rowIndex + 2 , rowIndex + 2 , 2 , 5 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 2 ) . GetCell ( 2 ) . SetCellValue ( "广东华泰检测科技有限公司" ) ;
region = new CellRangeAddress ( rowIndex + 2 , rowIndex + 2 , 6 , 7 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 2 ) . GetCell ( 6 ) . SetCellValue ( "委托单编号" ) ;
region = new CellRangeAddress ( rowIndex + 2 , rowIndex + 2 , 8 , 12 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 2 ) . GetCell ( 8 ) . SetCellValue ( tbTitle . Rows [ 0 ] [ "HotHardCode" ] . ToString ( ) ) ;
//行4
region = new CellRangeAddress ( rowIndex + 3 , rowIndex + 3 , 0 , 1 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 3 ) . GetCell ( 0 ) . SetCellValue ( "接收人" ) ;
region = new CellRangeAddress ( rowIndex + 3 , rowIndex + 3 , 2 , 5 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 3 ) . GetCell ( 2 ) . SetCellValue ( "" ) ;
region = new CellRangeAddress ( rowIndex + 3 , rowIndex + 3 , 6 , 7 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 3 ) . GetCell ( 6 ) . SetCellValue ( "检件名称" ) ;
region = new CellRangeAddress ( rowIndex + 3 , rowIndex + 3 , 8 , 12 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 3 ) . GetCell ( 8 ) . SetCellValue ( "管道焊缝" ) ;
//行5
region = new CellRangeAddress ( rowIndex + 4 , rowIndex + 4 , 0 , 1 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 4 ) . GetCell ( 0 ) . SetCellValue ( "检测数量" ) ;
region = new CellRangeAddress ( rowIndex + 4 , rowIndex + 4 , 2 , 5 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 4 ) . GetCell ( 2 ) . SetCellValue ( tb . Rows . Count . ToString ( ) ) ;
region = new CellRangeAddress ( rowIndex + 4 , rowIndex + 4 , 6 , 7 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 4 ) . GetCell ( 6 ) . SetCellValue ( "表面质量" ) ;
region = new CellRangeAddress ( rowIndex + 4 , rowIndex + 4 , 8 , 12 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 4 ) . GetCell ( 8 ) . SetCellValue ( "合格" ) ;
//行6
region = new CellRangeAddress ( rowIndex + 5 , rowIndex + 5 , 0 , 1 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 5 ) . GetCell ( 0 ) . SetCellValue ( "检测时机" ) ;
region = new CellRangeAddress ( rowIndex + 5 , rowIndex + 5 , 2 , 5 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 5 ) . GetCell ( 2 ) . SetCellValue ( "" ) ;
region = new CellRangeAddress ( rowIndex + 5 , rowIndex + 5 , 6 , 7 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 5 ) . GetCell ( 6 ) . SetCellValue ( "检测标准" ) ;
region = new CellRangeAddress ( rowIndex + 5 , rowIndex + 5 , 8 , 12 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 5 ) . GetCell ( 8 ) . SetCellValue ( tbTitle . Rows [ 0 ] [ "TestStandard" ] . ToString ( ) ) ;
//行7
region = new CellRangeAddress ( rowIndex + 6 , rowIndex + 6 , 0 , 1 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 6 ) . GetCell ( 0 ) . SetCellValue ( "检测比例" ) ;
region = new CellRangeAddress ( rowIndex + 6 , rowIndex + 6 , 2 , 3 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 6 ) . GetCell ( 2 ) . SetCellValue ( "100%" ) ;
region = new CellRangeAddress ( rowIndex + 6 , rowIndex + 6 , 4 , 5 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 6 ) . GetCell ( 4 ) . SetCellValue ( "检测方法" ) ;
region = new CellRangeAddress ( rowIndex + 6 , rowIndex + 6 , 6 , 7 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 6 ) . GetCell ( 6 ) . SetCellValue ( "布氏硬度" ) ;
region = new CellRangeAddress ( rowIndex + 6 , rowIndex + 6 , 8 , 10 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 6 ) . GetCell ( 8 ) . SetCellValue ( "合格值" ) ;
region = new CellRangeAddress ( rowIndex + 6 , rowIndex + 6 , 11 , 12 ) ;
ws . AddMergedRegion ( region ) ;
2025-02-26 12:42:11 +08:00
ws . GetRow ( rowIndex + 6 ) . GetCell ( 11 ) . SetCellValue ( tbTitle . Rows [ 0 ] [ "STE_Code" ] . ToString ( ) ) ;
2025-01-13 16:12:34 +08:00
//行8
region = new CellRangeAddress ( rowIndex + 7 , rowIndex + 7 , 0 , 0 ) ;
ws . GetRow ( rowIndex + 7 ) . GetCell ( 0 ) . SetCellValue ( "序号" ) ;
region = new CellRangeAddress ( rowIndex + 7 , rowIndex + 7 , 1 , 4 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 7 ) . GetCell ( 1 ) . SetCellValue ( "设备部位/管线编号" ) ;
region = new CellRangeAddress ( rowIndex + 7 , rowIndex + 7 , 5 , 6 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 7 ) . GetCell ( 5 ) . SetCellValue ( "材质" ) ;
region = new CellRangeAddress ( rowIndex + 7 , rowIndex + 7 , 7 , 9 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 7 ) . GetCell ( 7 ) . SetCellValue ( "规格mm" ) ;
region = new CellRangeAddress ( rowIndex + 7 , rowIndex + 7 , 10 , 11 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( rowIndex + 7 ) . GetCell ( 10 ) . SetCellValue ( "焊口号" ) ;
region = new CellRangeAddress ( rowIndex + 7 , rowIndex + 7 , 12 , 12 ) ;
ws . GetRow ( rowIndex + 7 ) . GetCell ( 12 ) . SetCellValue ( "备注" ) ;
#endregion
}
else
{
var pNum = ( i - 1 ) * 38 ;
//取数据开始和结束条数
dStart = 27 + ( ( i - 2 ) * 38 ) ;
dEnd = 27 + pNum ;
//数据开始行和结束行
tStart = rowIndex ;
tEnd = rowIndex + 38 ;
}
#endregion
#region 数 据
//创建数据行和列
ws = ExcelCreateRow ( ws , hssfworkbook , tStart , tEnd , style , 0 , 12 ) ;
////获取当前页数据
var pageTb = GetPageToTable ( tb , dStart , dEnd ) ;
////遍历数据
for ( int j = 0 ; j < pageTb . Rows . Count ; j + + )
{
int dataRow = tStart + j ;
ws . GetRow ( dataRow ) . GetCell ( 0 ) . SetCellValue ( pageTb . Rows [ j ] [ "Number" ] . ToString ( ) ) ;
ws . GetRow ( dataRow ) . GetCell ( 1 ) . SetCellValue ( pageTb . Rows [ j ] [ "ISO_IsoNo" ] . ToString ( ) ) ;
ws . GetRow ( dataRow ) . GetCell ( 5 ) . SetCellValue ( pageTb . Rows [ j ] [ "STE_Code" ] . ToString ( ) ) ;
ws . GetRow ( dataRow ) . GetCell ( 7 ) . SetCellValue ( pageTb . Rows [ j ] [ "JOT_JointDesc" ] . ToString ( ) ) ;
ws . GetRow ( dataRow ) . GetCell ( 10 ) . SetCellValue ( pageTb . Rows [ j ] [ "JOT_JointNo" ] . ToString ( ) ) ;
ws . GetRow ( dataRow ) . GetCell ( 12 ) . SetCellValue ( pageTb . Rows [ j ] [ "RemarkStr" ] . ToString ( ) ) ;
}
#endregion
2025-02-26 12:42:11 +08:00
#region 尾 部
//创建尾部行
ws = ExcelCreateRowTitle ( ws , hssfworkbook , tEnd + 1 , tEnd + 2 , style , 0 , 12 ) ;
ICellStyle endStyleOne = CommonPrint . SetExcelStyle ( hssfworkbook , BorderStyle . Thin , BorderStyle . Thin , BorderStyle . Thin , BorderStyle . Thin , VerticalAlignment . Center , HorizontalAlignment . Left , 10 ) ;
//创建尾部列
region = new CellRangeAddress ( tEnd + 1 , tEnd + 2 , 0 , 5 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( tEnd + 1 ) . GetCell ( 0 ) . CellStyle = endStyleOne ;
ws . GetRow ( tEnd + 1 ) . GetCell ( 0 ) . SetCellValue ( "委托单位:镇海石化建安工程股份有限公司" ) ;
region = new CellRangeAddress ( tEnd + 1 , tEnd + 1 , 6 , 12 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( tEnd + 1 ) . GetCell ( 6 ) . CellStyle = endStyleOne ;
ws . GetRow ( tEnd + 1 ) . GetCell ( 6 ) . SetCellValue ( "委托人:" ) ;
region = new CellRangeAddress ( tEnd + 2 , tEnd + 2 , 6 , 12 ) ;
ws . AddMergedRegion ( region ) ;
ws . GetRow ( tEnd + 2 ) . GetCell ( 6 ) . CellStyle = endStyleOne ;
ws . GetRow ( tEnd + 2 ) . GetCell ( 6 ) . SetCellValue ( "日期: 年 月 日" ) ;
#endregion
rowIndex = tEnd + 4 ;
2025-01-13 16:12:34 +08:00
}
}
}
}
ws . SetMargin ( MarginType . LeftMargin , 0.6 ) ;
ws . SetMargin ( MarginType . RightMargin , 0.2 ) ;
//ws.SetMargin(MarginType.BottomMargin, 1);
//ws.SetMargin(MarginType.TopMargin, 1);
ws . PrintSetup . Landscape = true ;
ws . PrintSetup . PaperSize = 9 ;
ws . ForceFormulaRecalculation = true ;
using ( FileStream filess = File . OpenWrite ( ReportFileName ) )
{
hssfworkbook . Write ( filess ) ;
}
FileInfo filet = new FileInfo ( ReportFileName ) ;
Response . Clear ( ) ;
Response . Charset = "GB2312" ;
Response . ContentEncoding = System . Text . Encoding . UTF8 ;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response . AddHeader ( "Content-Disposition" , "attachment; filename=" + Server . UrlEncode ( "硬度检测委托单.xlsx" ) ) ;
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response . AddHeader ( "Content-Length" , filet . Length . ToString ( ) ) ;
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response . ContentType = "application/ms-excel" ;
// 把文件流发送到客户端
Response . WriteFile ( filet . FullName ) ;
// 停止页面的执行
Response . End ( ) ;
}
}
#endregion
#region 私 有 方 法
/// <summary>
/// 行和列
/// </summary>
/// <returns></returns>
private XSSFSheet ExcelCreateRowTitle ( XSSFSheet ws , XSSFWorkbook hssfworkbook , int sRows , int eRows , ICellStyle style , int cStart , int cEnd )
{
for ( int i = sRows ; i < = eRows ; i + + )
{
ws . CreateRow ( i ) ;
ws . GetRow ( i ) . HeightInPoints = i = = sRows ? 30
: i = = ( sRows + 1 ) ? 27 :
i = = ( sRows + 2 ) ? 28 : 24 ;
for ( int j = cStart ; j < = cEnd ; j + + )
{
ws . GetRow ( i ) . CreateCell ( j ) ;
ws . GetRow ( i ) . GetCell ( j ) . CellStyle = style ;
}
}
return ws ;
}
/// <summary>
/// 数据行
/// </summary>
/// <returns></returns>
private XSSFSheet ExcelCreateRow ( XSSFSheet ws , XSSFWorkbook hssfworkbook , int sRows , int eRows , ICellStyle style , int cStart , int cEnd )
{
CellRangeAddress region ;
for ( int i = sRows ; i < = eRows ; i + + )
{
ws . CreateRow ( i ) ;
ws . GetRow ( i ) . HeightInPoints = 24f ;
for ( int j = cStart ; j < = cEnd ; j + + )
{
ws . GetRow ( i ) . CreateCell ( j ) ;
ws . GetRow ( i ) . GetCell ( j ) . CellStyle = style ;
}
region = new CellRangeAddress ( i , i , 1 , 4 ) ;
ws . AddMergedRegion ( region ) ;
region = new CellRangeAddress ( i , i , 5 , 6 ) ;
ws . AddMergedRegion ( region ) ;
region = new CellRangeAddress ( i , i , 7 , 9 ) ;
ws . AddMergedRegion ( region ) ;
region = new CellRangeAddress ( i , i , 10 , 11 ) ;
ws . AddMergedRegion ( region ) ;
}
return ws ;
}
/// <summary>
/// 查询指定条数分页
/// </summary>
/// <returns></returns>
public static DataTable GetPageToTable ( DataTable dt , int StartNum , int EndNum )
{
//0页代表每页数据, 直接返回
if ( EndNum = = 0 ) return dt ;
//数据源为空返回空DataTable
if ( dt = = null ) return new DataTable ( ) ;
DataTable newdt = dt . Copy ( ) ;
newdt . Clear ( ) ; //copy dt的框架
if ( StartNum > = dt . Rows . Count )
return newdt ; //源数据记录数小于等于要显示的记录, 直接返回dt
if ( EndNum > dt . Rows . Count )
EndNum = dt . Rows . Count ;
for ( int i = StartNum ; i < = EndNum - 1 ; i + + )
{
DataRow newdr = newdt . NewRow ( ) ;
DataRow dr = dt . Rows [ i ] ;
foreach ( DataColumn column in dt . Columns )
{
newdr [ column . ColumnName ] = dr [ column . ColumnName ] ;
}
newdt . Rows . Add ( newdr ) ;
}
return newdt ;
}
#endregion
}
}