c# - npoi 垂直对齐中心

标签 c# excel npoi

我已经尝试了十几种方法来做到这一点,但没有任何效果。我尝试将垂直对齐应用于居中。

似乎没有任何效果。

我非常感谢您的帮助。

这是我的代码:

        var workbook = new HSSFWorkbook();

            var sheet = workbook.CreateSheet("Zmiana " + i.ToString());

            var headerRow = sheet.CreateRow(0);

            headerRow.CreateCell(0).SetCellValue("Data");
            headerRow.CreateCell(1).SetCellValue("Maszyna");
            headerRow.CreateCell(2).SetCellValue("Zmiana");
            headerRow.CreateCell(3).SetCellValue("Brygadzista");

            int rowNumber = 1;

            List<MachineStatusReport> listForOneShift = list.Where(c => c.Zmiana == i).ToList();

            foreach (MachineStatusReport elements in listForOneShift)
            {
                var row = sheet.CreateRow(rowNumber++);


                    row.CreateCell(0).SetCellValue(date.ToShortDateString());
                    row.CreateCell(1).SetCellValue(elements.Stanowisko);
                    row.CreateCell(2).SetCellValue("Zmiana " + i.ToString());
                    row.CreateCell(3).SetCellValue(elements.Brygadzista);
                    row.CreateCell(4).SetCellValue(elements.KodProduktu); 
            }

                    NPOI.SS.Util.CellRangeAddress cra = new NPOI.SS.Util.CellRangeAddress(1, counter, 1, 5);
                    sheet.AddMergedRegion(cra);
            }

        MemoryStream output = new MemoryStream();
        workbook.Write(output);

干杯!

最佳答案

以下代码主要取自 \examples 文件夹,结果为:

Excel demo

(截取自 Excel 2007 的屏幕截图)

刚刚安装最新下载 - 版本 2.0 Beta 1 [v2.0.1] 2013 年 2 月 (NPOI.DLL 2.3.0.0)

/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

/* ================================================================
 * Author: Tony Qu 
 * Author's email: tonyqus (at) gmail.com 
 * NPOI HomePage: http://www.codeplex.com/npoi
 * Contributors:
 * 
 * ==============================================================*/

using System;
using System.Text;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;

/*
 This sample is copied from poi.hssf.usermodel.examples. Original name is Borders.java
 */
namespace SetBorderStyleInXls
{
    class Program
    {
        static void Main(string[] args)
        {
            InitializeWorkbook();

            ISheet sheet = hssfworkbook.CreateSheet("new sheet");

            // Create a row and put some cells in it. Rows are 0 based.
            IRow row = sheet.CreateRow(1);

            // Create a cell and put a value in it.
            ICell cell = row.CreateCell(1);
            cell.SetCellValue(4);

            // Style the cell with borders all around.
            ICellStyle style = hssfworkbook.CreateCellStyle();
            style.BorderBottom= BorderStyle.Thin;
            style.BottomBorderColor= HSSFColor.Black.Index;
            style.BorderLeft = BorderStyle.DashDotDot;
            style.LeftBorderColor= HSSFColor.Green.Index;
            style.BorderRight = BorderStyle.Hair;
            style.RightBorderColor= HSSFColor.Blue.Index;
            style.BorderTop = BorderStyle.MediumDashed;
            style.TopBorderColor= HSSFColor.Orange.Index;
            cell.CellStyle= style;

            // Create a cell and put a value in it.
            ICell cell2 = row.CreateCell(2);
            cell2.SetCellValue(5);
            ICellStyle style2 = hssfworkbook.CreateCellStyle();
            style2.BorderBottom = BorderStyle.Thick;
            style.BottomBorderColor = HSSFColor.Black.Index;
            cell2.CellStyle = style2;

            // Create a vertically and horizontally centred cell
            row.CreateCell(3).SetCellValue("Center Hello World Hello WorldHello WorldHello WorldHello WorldHello World");
            ICellStyle styleMiddle = hssfworkbook.CreateCellStyle();
            styleMiddle.Alignment = HorizontalAlignment.Center;
            styleMiddle.VerticalAlignment = VerticalAlignment.Center;
            styleMiddle.WrapText = true; //wrap the text in the cell
            row.GetCell(3).CellStyle = styleMiddle;
            sheet.SetColumnWidth(3, 256 * 40);

            // Create a vertically aligned top and horizontally centred cell
            row.CreateCell(4).SetCellValue("Top Hello World Hello WorldHello WorldHello WorldHello WorldHello World");
            ICellStyle styleMiddle2 = hssfworkbook.CreateCellStyle();
            styleMiddle2.Alignment = HorizontalAlignment.Center;
            styleMiddle2.VerticalAlignment = VerticalAlignment.Top;
            styleMiddle2.WrapText = true; //wrap the text in the cell
            row.GetCell(4).CellStyle = styleMiddle2;
            sheet.SetColumnWidth(4, 256 * 40);

            row.Height = 1000;

            WriteToFile();
        }


        static HSSFWorkbook hssfworkbook;

        static void WriteToFile()
        {
            //Write the stream data of workbook to the root directory
            FileStream file = new FileStream(@"test.xls", FileMode.Create);
            hssfworkbook.Write(file);
            file.Close();
        }

        static void InitializeWorkbook()
        {
            hssfworkbook = new HSSFWorkbook();

            //Create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "NPOI Team";
            hssfworkbook.DocumentSummaryInformation = dsi;

            //Create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "NPOI SDK Example";
            hssfworkbook.SummaryInformation = si;
        }
    }
}

关于c# - npoi 垂直对齐中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276224/

相关文章:

c# - ASP.NET MVC 3 Controller 会抛出异常,即使我有麻烦吗?

c# - GC.KeepAlive 与使用

Excel countif 和 sumif 一起使用

c# - 尝试使用 NPOI 创建一个新的 .xlsx 文件并写入

c# - 带有 NPOI 的 .xlsx 文件损坏问题 - Excel 无法打开文件 'file.xlsx",因为文件格式或文件扩展名无效

c# - 小巧玲珑.NET : The varchar(4000) default

c# - Winforms - 引用可更新的配置文件

c# - 如何在 OpenXML 中为格式为 (#,###.##) 的小数创建 CellValue

excel - 使用 VBA 设置 Sharepoint 标签/属性

c# - NPOI 将字体应用于整行单元格