Beispiel #1
0
        }//解压结束

        /// <summary>
        /// 初始化导出表格样式
        /// </summary>
        /// LZ Add 2016-08-04
        /// <param name="book">Excel文件</param>
        /// <param name="param"></param>
        /// <param name="paramComment">批注字典 注意:字典的key要与表头key一致</param>
        /// <returns></returns>
        public static ISheet CreateSheet(HSSFWorkbook book, Dictionary <string, int> param, Dictionary <string, string> paramComment, string sheetName = "Sheet1")
        {
            //添加一个sheet
            ISheet sheet1 = book.CreateSheet(sheetName);
            IRow   row    = sheet1.CreateRow(0);


            //初始化样式
            ICellStyle mStyle = book.CreateCellStyle();

            mStyle.Alignment         = HorizontalAlignment.Center;
            mStyle.VerticalAlignment = VerticalAlignment.Center;
            IFont mfont = book.CreateFont();

            mfont.FontHeight = 10 * 20;
            mStyle.SetFont(mfont);

            HSSFPatriarch patr = sheet1.CreateDrawingPatriarch() as HSSFPatriarch;

            NPOI.SS.UserModel.ICreationHelper facktory = book.GetCreationHelper();
            HSSFComment comment = null;

            NPOI.SS.UserModel.IClientAnchor anchor = facktory.CreateClientAnchor();

            int i = 0;

            foreach (var item in param)
            {
                //设置列宽
                sheet1.SetColumnWidth(i, item.Value * 256);
                sheet1.SetDefaultColumnStyle(i, mStyle);
                row.CreateCell(i).SetCellValue(item.Key.ToString());

                if (paramComment.ContainsKey(item.Key.ToString()))
                {
                    //设置批注
                    anchor                     = facktory.CreateClientAnchor();
                    anchor.Col1                = row.GetCell(i).ColumnIndex;
                    anchor.Col2                = row.GetCell(i).ColumnIndex + 1;
                    anchor.Row1                = row.RowNum;
                    anchor.Row2                = row.RowNum + 3;
                    comment                    = patr.CreateCellComment(anchor) as HSSFComment;
                    comment.String             = new HSSFRichTextString(paramComment[item.Key.ToString()].ToString());
                    comment.Author             = ("CySoft");
                    row.GetCell(i).CellComment = (comment);
                }
                i++;
            }
            i = 0;
            sheet1.GetRow(0).Height = 28 * 20;
            return(sheet1);
        }