示例#1
0
        /// <summary>
        /// 根据类型获取单元格样式
        /// </summary>
        /// <param name="cellStyleType"></param>
        /// <returns></returns>
        public static ICellStyle GetCellStyleByType(HSSFWorkbook workbook, CellStyleType cellStyleType)
        {
#warning 检测是否已创建样式
            var cellstyle = workbook.CreateCellStyle();
            switch (cellStyleType)
            {
            case CellStyleType.Header:
                #region 设置列头单元格样式
                //创建单元格样式
                cellstyle.Alignment         = HorizontalAlignment.Center;
                cellstyle.VerticalAlignment = VerticalAlignment.Center;
                //创建字体
                var headerFont = workbook.CreateFont();
                headerFont.IsBold             = true;
                headerFont.FontHeightInPoints = 12;
                cellstyle.SetFont(headerFont);
                #endregion
                break;

            case CellStyleType.Common:
                cellstyle.Alignment         = HorizontalAlignment.Center;
                cellstyle.VerticalAlignment = VerticalAlignment.Center;
                cellstyle.WrapText          = true;
                //文本缩进
                cellstyle.Indention = 3;
                //文本方向  逆时针旋转 -90~90
                cellstyle.Rotation = (short)90;
                break;

            case CellStyleType.DateTime:
                cellstyle.DataFormat = workbook.CreateDataFormat().GetFormat("yyyy年mm月dd日");
                break;

            case CellStyleType.Float:
                //百分比:0.00%
                //数字转中文(限整数):[DbNum2][$-804]0
                //科学计数法:0.00E+00
                cellstyle.DataFormat = workbook.CreateDataFormat().GetFormat("0.00");
                break;

            case CellStyleType.Money:
                cellstyle.DataFormat = workbook.CreateDataFormat().GetFormat("¥#,##0");
                break;

            case CellStyleType.Border:
                cellstyle.BorderTop       = BorderStyle.Dashed;
                cellstyle.BorderRight     = BorderStyle.Dotted;
                cellstyle.BorderBottom    = BorderStyle.Thin;
                cellstyle.BorderLeft      = BorderStyle.Double;
                cellstyle.BorderDiagonal  = BorderDiagonal.Forward;
                cellstyle.LeftBorderColor = HSSFColor.Red.Index;
                break;
            }

            return(cellstyle);
        }
示例#2
0
        public void SetCellValue(string strSheetName, int intRowNumber, int intCellNumber, object strCellValue, CellStyleType styleType)
        {
            try
            {
                ISheet sheet;
                sheet = _workbook.GetSheet(strSheetName);
                IRow row = sheet.GetRow(intRowNumber);
                if (row == null) //如果未取到,则创建行
                {
                    row = sheet.CreateRow(intRowNumber);
                    if (RowHeight != 0)
                    {
                        row.Height = RowHeight;
                    }
                }
                HSSFCell cell;
                if (strCellValue != null) //如果为空,则不设置数据值
                {
                    cell = (HSSFCell)row.CreateCell(intCellNumber);
                    //cell.SetCellValue(strCellValue);

                    if (strCellValue is string)
                    {
                        cell.SetCellValue(strCellValue.ToString());
                    }
                    if (strCellValue is HSSFRichTextString)
                    {
                        cell.SetCellValue((HSSFRichTextString)strCellValue);
                    }
                }
                else
                {
                    cell = (HSSFCell)row.CreateCell(intCellNumber);
                }

                switch (styleType)
                {
                case CellStyleType.Caption:
                    cell.CellStyle = _hssfCaptionCellStyle;
                    break;

                case CellStyleType.SubCaption:
                    cell.CellStyle = _hssfSubCaptionCellStyle;
                    break;

                case CellStyleType.Data:
                    cell.CellStyle = _hssfDataCellStyle;
                    break;

                case CellStyleType.TableHead:
                    cell.CellStyle = _hssfTableHeadStyle;
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                string message = ex.ToString();
            }
        }
示例#3
0
 public void SetValue(string strSheetName, int intRowNumber, int intCellNumber, object strCellValue, CellStyleType styleType)
 {
     throw new NotImplementedException();
 }