云智博客-blog

云智企业管理信息系统官方博客

nginx在云智系统和云智博客使用

云智系统和博客使用的是阿里云的服务,目前购买了ECS和RDS等服务.

1.ssl证书加载在nginx上,使用的是GeoTrust通配符域名专业版OV SSL证书类型,可对所有的二级域名(*.ciemis.com)域名提供服务

2.启用了http2

3.云智系统(www.ciemis.com)可以使用http和https两种方式访问.云智博客(blog.ciemis.com)只支持https访问,http访问是会自动通过301跳转到https.当然也可以通过javascript进行跳转,代码如下:

var ishttps = 'https:' == document.location.protocol ? true : false;
if (!ishttps) {
    window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);
}

4.为了将多个ciemis.com的二级域名绑定到同一台服务器上,我使用了nginx作为反向代理.

www.ciemis.com在IIS上使用的是3017端口,blog.ciemis.com在IIS上使用的是3000端口, 然后在ngnix上配置了反向代理.

当然直接在IIS上绑定主机头也是可行的.

nginx的配置文件节选如下:

#云智
server {
    #监听端口
    listen       80;
    #自己指定要跳转的域名
    server_name  www.ciemis.com;
		        
    #反向代理配置
    location / {
        proxy_pass     http://localhost:3017;
        proxy_set_header   X-Real-IP $remote_addr;
    }
}

server {
    #监听端口
    listen       443 ssl http2;
    #自己指定要跳转的域名
    server_name  www.ciemis.com;
		
    ssl                  on; 
    ssl_certificate   cert/214xxxxxx0803.pem;
    ssl_certificate_key  cert/214xxxxxx0803.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
        
    #反向代理配置
    location / {
        proxy_pass     http://localhost:3017;
        proxy_set_header   X-Real-IP $remote_addr;
    }
}
	
#云智blog
server {
    #监听端口
    listen       80;
    #自己指定要跳转的域名
    server_name  blog.ciemis.com;
		        
    #转向https
    return  301 https://$host;
}
	
server {
    #监听端口
    listen       443 ssl http2;
    #自己指定要跳转的域名
    server_name  blog.ciemis.com;
		
    ssl                  on; 
    ssl_certificate   cert/214xxxxxx0803.pem;
    ssl_certificate_key  cert/214xxxxxx0803.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
        
    #反向代理配置
    location / {
        proxy_pass     http://localhost:3000;
        proxy_set_header   X-Real-IP $remote_addr;
    }
}

附nginx的启动,重启,关闭命令

start nginx #启动nginx

nginx -s reload  #修改配置后重新加载生效
nginx -s reopen  #重新打开日志文件
nginx -t -c /path/to/nginx.conf  #测试nginx配置文件是否正确

#关闭nginx:
nginx -s stop  #快速停止nginx
         quit  #完整有序的停止nginx

 

NPOI在云智系统中的使用

在云智系统中,有许多的数据需要导入到excel,使用微软的office对于web来说要求对服务器的权限过高,所以我使用的第三方的开源软件NPOI.

下面的代码示例就是直接将DataTable转为Excel.表头通过titleArr进行定义.

/// <summary>  
/// 导出Excel  
/// </summary>  
/// <param name="Dt">数据源</param>  
/// <param name="ExcleName">导入文件名称</param>  
/// <param name="SheetName">工作薄名称</param>  
/// <param name="titleArr">标题栏</param>  
/// <param name="clumnArr">栏位名</param>  
public static void ResponseDataSetToExcel(DataTable Dt, string ExcleName, string SheetName, string[] titleArr, string[] clumnArr)
{
    HSSFWorkbook hssfworkbook = new HSSFWorkbook();

    DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
    dsi.Company = "Ciemis";
    hssfworkbook.DocumentSummaryInformation = dsi;

    //create a entry of SummaryInformation  
    SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
    si.Subject = " Export  Excel";
    hssfworkbook.SummaryInformation = si;

    HSSFSheet excelSheet = (HSSFSheet)hssfworkbook.CreateSheet(SheetName);
    int rowCount = 0;
    HSSFRow newRow = (HSSFRow)excelSheet.CreateRow(0);
    rowCount++;
    //循环写出列头           
    for (int i = 0; i < titleArr.Length; i++)
    {
        HSSFCell newCell = (HSSFCell)newRow.CreateCell(i);
        newCell.SetCellValue(titleArr[i]);
    }
    for (int i = 0; i < Dt.Rows.Count; i++)
    {
        rowCount++;
        HSSFRow newRowData = (HSSFRow)excelSheet.CreateRow(rowCount);
        DataRow dr = Dt.Rows[i];
        for (int j = 0; j < clumnArr.Length; j++)
        {
            HSSFCell newCell = (HSSFCell)newRow.CreateCell(rowCount);
            newCell.SetCellValue(dr[titleArr[j]].ToString());
        }
    }

    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
    //避免乱码System.Text.Encoding.UTF8
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(ExcleName, System.Text.Encoding.UTF8));
    HttpContext.Current.Response.Clear();

    MemoryStream file = new MemoryStream();
    hssfworkbook.Write(file);

    HttpContext.Current.Response.BinaryWrite(file.GetBuffer());
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();
}

要导出一个DataSet,里面的多个DataTable对应Excel的多个Sheet;

且自动根据数据类型设置Excel的单元格格式.

/// <summary>
/// 导出Excel  
/// </summary>
/// <param name="sourceDs"></param>
/// <param name="fileName">导出的文件名</param>
/// <param name="sheetName">导出的表名,多个表请以逗号(,)分割</param>
public static void ResponseDataSetToExcel(DataSet sourceDs, string fileName, string sheetName = null)
{
    fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).ToString();

    HSSFWorkbook workbook = new HSSFWorkbook();
    MemoryStream ms = new MemoryStream();
    string[] sheetNames = (sheetName ?? string.Empty).Split(',');
    int length = sourceDs.Tables.Count;
    for (int i = 0; i < length; i++)
    {
        var _sheetName = sheetNames.Length > i ? sheetNames[i] : string.Format("Sheet{0}", i);
        var sheet = workbook.CreateSheet(_sheetName);
        sheet.CreateFreezePane(0, 1, 0, 1);
        //headStyle
        var headStyle = workbook.CreateCellStyle();
        IFont font = workbook.CreateFont();
        font.Boldweight = (short)FontBoldWeight.Bold;
        font.FontHeightInPoints = (short)11;
        font.Color = HSSFColor.Black.Index;
        headStyle.FillBackgroundColor = HSSFColor.Black.Index;
        //headStyle.FillPattern = FillPattern.SolidForeground;
        headStyle.SetFont(font);
        //cellStyleDet
        var cellStyleDet = workbook.CreateCellStyle();
        IFont font1 = workbook.CreateFont();
        font.FontHeightInPoints = (short)10;
        cellStyleDet.SetFont(font1);

        var headerRow = sheet.CreateRow(0);
        // handling header.
        foreach (DataColumn column in sourceDs.Tables[i].Columns)
        {
            var headCell = headerRow.CreateCell(column.Ordinal);
            headCell.SetCellValue(column.ColumnName);
            headCell.CellStyle = headStyle;
        }
        // handling value.
        int rowIndex = 1;
        foreach (DataRow row in sourceDs.Tables[i].Rows)
        {
            var dataRow = sheet.CreateRow(rowIndex);
            foreach (DataColumn column in sourceDs.Tables[i].Columns)
            {
                var detCell = dataRow.CreateCell(column.Ordinal);
                var cellValue = row[column].ToString();
                var type = column.DataType.ToString().ToLower();
                if (row[column] != DBNull.Value)
                {
                    if (type.Contains("decimal") || type.Contains("double") || type.Contains("int"))
                    {
                        detCell.SetCellValue(Convert.ToDouble(row[column]));
                    }
                    else if (type.Contains("datetime"))
                    {
                        detCell.SetCellValue(((DateTime)row[column]).ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                    else
                    {
                        detCell.SetCellValue(row[column].ToString());
                    }
                }
                detCell.CellStyle = cellStyleDet;
            }
            rowIndex++;
        }
    }
    workbook.Write(ms);

    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
    HttpContext.Current.Response.BinaryWrite(ms.GetBuffer());
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();

    ms.Flush();
    ms.Position = 0;
    ms.Close();
    workbook = null;
    ms = null;
}

如果想对excel进行只读处理,不许对导出的数据进行修改,只需要加入一行代码:

sheet.ProtectSheet("password");