[Test]//(expected=RuntimeException.class) public void SetCellStylePropertyWithInvalidValue() { IWorkbook wb = _testDataProvider.CreateWorkbook(); ISheet s = wb.CreateSheet(); IRow r = s.CreateRow(0); ICell c = r.CreateCell(0); // An invalid BorderStyle constant CellUtil.SetCellStyleProperty(c, CellUtil.BORDER_BOTTOM, 42); wb.Close(); }
public void SetCellStylePropertyBorderWithShortAndEnum() { IWorkbook wb = _testDataProvider.CreateWorkbook(); ISheet s = wb.CreateSheet(); IRow r = s.CreateRow(0); ICell c = r.CreateCell(0); // A valid BorderStyle constant, as a Short CellUtil.SetCellStyleProperty(c, CellUtil.BORDER_BOTTOM, (short)BorderStyle.DashDot); Assert.AreEqual(BorderStyle.DashDot, c.CellStyle.BorderBottom); // A valid BorderStyle constant, as an Enum CellUtil.SetCellStyleProperty(c, CellUtil.BORDER_TOP, BorderStyle.MediumDashDot); Assert.AreEqual(BorderStyle.MediumDashDot, c.CellStyle.BorderTop); wb.Close(); }
private void copyDataAndFormat(ICell srcCell, ICell dstCell) { // コピー元のCellType(文字列とか数値とか)による分類 switch (srcCell.CellType) { // 文字列 case CellType.String: dstCell.SetCellValue(sanitize(srcCell.ToString())); break; // 数値・通貨 case CellType.Numeric: // 日付を含むことがある if (DateUtil.IsCellDateFormatted(srcCell)) { dstCell.SetCellValue(srcCell.DateCellValue); } else { dstCell.SetCellValue(srcCell.NumericCellValue); } break; // 真偽値 case CellType.Boolean: dstCell.SetCellValue(srcCell.BooleanCellValue); break; // そのほかは文字列型とみなす default: dstCell.SetCellValue(sanitize(srcCell.ToString())); break; } // CellTypeをコピー dstCell.SetCellType(srcCell.CellType); // スタイルをコピー var srcCellStyle = srcCell.CellStyle; CellUtil.SetCellStyleProperty(dstCell, CellUtil.DATA_FORMAT, srcCellStyle.DataFormat); }
public void SetCellStyleProperty() { IWorkbook wb = _testDataProvider.CreateWorkbook(); ISheet s = wb.CreateSheet(); IRow r = s.CreateRow(0); ICell c = r.CreateCell(0); // Add a border should create a new style int styCnt1 = wb.NumCellStyles; CellUtil.SetCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.Thin); int styCnt2 = wb.NumCellStyles; Assert.AreEqual(styCnt1 + 1, styCnt2); // Add same border to another cell, should not create another style c = r.CreateCell(1); CellUtil.SetCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.Thin); int styCnt3 = wb.NumCellStyles; Assert.AreEqual(styCnt2, styCnt3); wb.Close(); }
public void TestBug55752() { IWorkbook wb = new XSSFWorkbook(); try { ISheet sheet = wb.CreateSheet("test"); for (int i = 0; i < 4; i++) { IRow row = sheet.CreateRow(i); for (int j = 0; j < 2; j++) { ICell cell = row.CreateCell(j); cell.CellStyle = (wb.CreateCellStyle()); } } // set content IRow row1 = sheet.GetRow(0); row1.GetCell(0).SetCellValue("AAA"); IRow row2 = sheet.GetRow(1); row2.GetCell(0).SetCellValue("BBB"); IRow row3 = sheet.GetRow(2); row3.GetCell(0).SetCellValue("CCC"); IRow row4 = sheet.GetRow(3); row4.GetCell(0).SetCellValue("DDD"); // merge cells CellRangeAddress range1 = new CellRangeAddress(0, 0, 0, 1); sheet.AddMergedRegion(range1); CellRangeAddress range2 = new CellRangeAddress(1, 1, 0, 1); sheet.AddMergedRegion(range2); CellRangeAddress range3 = new CellRangeAddress(2, 2, 0, 1); sheet.AddMergedRegion(range3); Assert.AreEqual(0, range3.FirstColumn); Assert.AreEqual(1, range3.LastColumn); Assert.AreEqual(2, range3.LastRow); CellRangeAddress range4 = new CellRangeAddress(3, 3, 0, 1); sheet.AddMergedRegion(range4); // set border RegionUtil.SetBorderBottom((int)BorderStyle.Thin, range1, sheet, wb); row2.GetCell(0).CellStyle.BorderBottom = BorderStyle.Thin; row2.GetCell(1).CellStyle.BorderBottom = BorderStyle.Thin; ICell cell0 = CellUtil.GetCell(row3, 0); CellUtil.SetCellStyleProperty(cell0, CellUtil.BORDER_BOTTOM, BorderStyle.Thin); ICell cell1 = CellUtil.GetCell(row3, 1); CellUtil.SetCellStyleProperty(cell1, CellUtil.BORDER_BOTTOM, BorderStyle.Thin); RegionUtil.SetBorderBottom((int)BorderStyle.Thin, range4, sheet, wb); // write to file Stream stream = new FileStream("55752.xlsx", FileMode.Create, FileAccess.ReadWrite); try { wb.Write(stream); } finally { stream.Close(); } } finally { wb.Close(); } }
public static void SetCellStyleProperty(ICell cell, HSSFWorkbook workbook, String propertyName, Object propertyValue) { CellUtil.SetCellStyleProperty(cell, propertyName, propertyValue); }