输出FeatureClass中的属性表

        /// 
        /// 输出要素类的属性表
        /// 
        /// 
        /// 
        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();
        }

 

你可能感兴趣的:(ArcEngine开发)