输出FeatureClass中的属性表

        /// <summary>
        /// 输出要素类的属性表
        /// </summary>
        /// <param name="pFeatureClass"></param>
        /// <param name="filePath"></param>
        public static void CreateExcelFile(IFeatureClass pFeatureClass, string filePath)
        {
            DataTable pDataTable = GetAttributesTable(pFeatureClass);
            NPOI.SS.UserModel.IWorkbook workbook = null;
            if (System.IO.Path.GetExtension(filePath) == ".xls")
            {
                workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
            }
            else
            {
                workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
            }

            // 创建行 
            NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("属性表");
            sheet.DefaultColumnWidth = 150 * 256;
            NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);
            row.HeightInPoints = 40;
            ICellStyle headerCellStyle = workbook.CreateCellStyle();
            headerCellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Lime.Index;
            headerCellStyle.FillPattern = FillPattern.SolidForeground;
            headerCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            headerCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            headerCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            headerCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;

            IFont headerFont = workbook.CreateFont();
            headerFont.Boldweight = (short)FontBoldWeight.Bold;
            headerFont.FontHeightInPoints = 14;
            headerCellStyle.SetFont(headerFont);
            headerCellStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中 方法1 
            headerCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CenterSelection;//设置居中 方法2

            NPOI.SS.UserModel.ICell cell = null;
            // 写入列名 
            int columnCount = pDataTable.Columns.Count;
            for (int i = 0; i < columnCount; i++)
            {
                cell = row.CreateCell(i);
                cell.CellStyle = headerCellStyle;
                cell.SetCellValue(pDataTable.Columns[i].ColumnName);
            }
            ICellStyle rowCellStyle = workbook.CreateCellStyle();
            rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            rowCellStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中 方法1 
            rowCellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CenterSelection;//设置居中 方法2
            // 写入列值 
            int rowCount = pDataTable.Rows.Count;
            for (int i = 0; i < rowCount; i++)
            {
                row = sheet.CreateRow(i + 1);
                for (int j = 0; j < columnCount; j++)
                {
                    cell = row.CreateCell(j);
                    cell.CellStyle = rowCellStyle;
                    cell.SetCellValue(pDataTable.Rows[i][j].ToString());
                }
            }
            // 写入文件 
            System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
            workbook.Write(fileStream);
            workbook.Close();
            fileStream.Close();
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值