本文整理汇总了C#中NPOI.XSSF.UserModel.XSSFWorkbook.GetSharedStringSource方法的典型用法代码示例。如果您正苦于以下问题:C# XSSFWorkbook.GetSharedStringSource方法的具体用法?C# XSSFWorkbook.GetSharedStringSource怎么用?C# XSSFWorkbook.GetSharedStringSource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NPOI.XSSF.UserModel.XSSFWorkbook
的用法示例。
在下文中一共展示了XSSFWorkbook.GetSharedStringSource方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestSaveLoadNew
public void TestSaveLoadNew()
{
XSSFWorkbook workbook = new XSSFWorkbook();
//check that the default date system is Set to 1900
CT_WorkbookPr pr = workbook.GetCTWorkbook().workbookPr;
Assert.IsNotNull(pr);
Assert.IsTrue(pr.IsSetDate1904());
Assert.IsFalse(pr.date1904, "XSSF must use the 1900 date system");
ISheet sheet1 = workbook.CreateSheet("sheet1");
ISheet sheet2 = workbook.CreateSheet("sheet2");
workbook.CreateSheet("sheet3");
IRichTextString rts = workbook.GetCreationHelper().CreateRichTextString("hello world");
sheet1.CreateRow(0).CreateCell((short)0).SetCellValue(1.2);
sheet1.CreateRow(1).CreateCell((short)0).SetCellValue(rts);
sheet2.CreateRow(0);
Assert.AreEqual(0, workbook.GetSheetAt(0).FirstRowNum);
Assert.AreEqual(1, workbook.GetSheetAt(0).LastRowNum);
Assert.AreEqual(0, workbook.GetSheetAt(1).FirstRowNum);
Assert.AreEqual(0, workbook.GetSheetAt(1).LastRowNum);
Assert.AreEqual(0, workbook.GetSheetAt(2).FirstRowNum);
Assert.AreEqual(0, workbook.GetSheetAt(2).LastRowNum);
FileInfo file = TempFile.CreateTempFile("poi-", ".xlsx");
Stream out1 = File.OpenWrite(file.Name);
workbook.Write(out1);
out1.Close();
// Check the namespace Contains what we'd expect it to
OPCPackage pkg = OPCPackage.Open(file.ToString());
PackagePart wbRelPart =
pkg.GetPart(PackagingUriHelper.CreatePartName("/xl/_rels/workbook.xml.rels"));
Assert.IsNotNull(wbRelPart);
Assert.IsTrue(wbRelPart.IsRelationshipPart);
Assert.AreEqual(ContentTypes.RELATIONSHIPS_PART, wbRelPart.ContentType);
PackagePart wbPart =
pkg.GetPart(PackagingUriHelper.CreatePartName("/xl/workbook.xml"));
// Links to the three sheets, shared strings and styles
Assert.IsTrue(wbPart.HasRelationships);
Assert.AreEqual(5, wbPart.Relationships.Size);
// Load back the XSSFWorkbook
workbook = new XSSFWorkbook(pkg);
Assert.AreEqual(3, workbook.NumberOfSheets);
Assert.IsNotNull(workbook.GetSheetAt(0));
Assert.IsNotNull(workbook.GetSheetAt(1));
Assert.IsNotNull(workbook.GetSheetAt(2));
Assert.IsNotNull(workbook.GetSharedStringSource());
Assert.IsNotNull(workbook.GetStylesSource());
Assert.AreEqual(0, workbook.GetSheetAt(0).FirstRowNum);
Assert.AreEqual(1, workbook.GetSheetAt(0).LastRowNum);
Assert.AreEqual(0, workbook.GetSheetAt(1).FirstRowNum);
Assert.AreEqual(0, workbook.GetSheetAt(1).LastRowNum);
Assert.AreEqual(0, workbook.GetSheetAt(2).FirstRowNum);
Assert.AreEqual(0, workbook.GetSheetAt(2).LastRowNum);
sheet1 = workbook.GetSheetAt(0);
Assert.AreEqual(1.2, sheet1.GetRow(0).GetCell(0).NumericCellValue, 0.0001);
Assert.AreEqual("hello world", sheet1.GetRow(1).GetCell(0).RichStringCellValue.String);
}
示例2: Bug57482
public void Bug57482()
{
foreach (PackageAccess access in new PackageAccess[] {
PackageAccess.READ_WRITE, PackageAccess.READ
})
{
FileInfo file = HSSFTestDataSamples.GetSampleFile("57482-OnlyNumeric.xlsx");
OPCPackage pkg = OPCPackage.Open(file, access);
try
{
XSSFWorkbook wb = new XSSFWorkbook(pkg);
Assert.IsNotNull(wb.GetSharedStringSource());
Assert.AreEqual(0, wb.GetSharedStringSource().Count);
DataFormatter fmt = new DataFormatter();
XSSFSheet s = wb.GetSheetAt(0) as XSSFSheet;
Assert.AreEqual("1", fmt.FormatCellValue(s.GetRow(0).GetCell(0)));
Assert.AreEqual("11", fmt.FormatCellValue(s.GetRow(0).GetCell(1)));
Assert.AreEqual("5", fmt.FormatCellValue(s.GetRow(4).GetCell(0)));
// Add a text cell
s.GetRow(0).CreateCell(3).SetCellValue("Testing");
Assert.AreEqual("Testing", fmt.FormatCellValue(s.GetRow(0).GetCell(3)));
// Try to Write-out and read again, should only work
// in Read-write mode, not Read-only mode
try
{
wb = XSSFTestDataSamples.WriteOutAndReadBack(wb) as XSSFWorkbook;
if (access == PackageAccess.READ)
Assert.Fail("Shouln't be able to write from Read-only mode");
}
catch (InvalidOperationException e)
{
if (access == PackageAccess.READ)
{
// Expected
}
else
{
// Shouldn't occur in Write-mode
throw e;
}
}
// Check again
s = wb.GetSheetAt(0) as XSSFSheet;
Assert.AreEqual("1", fmt.FormatCellValue(s.GetRow(0).GetCell(0)));
Assert.AreEqual("11", fmt.FormatCellValue(s.GetRow(0).GetCell(1)));
Assert.AreEqual("5", fmt.FormatCellValue(s.GetRow(4).GetCell(0)));
Assert.AreEqual("Testing", fmt.FormatCellValue(s.GetRow(0).GetCell(3)));
}
finally
{
pkg.Revert();
}
}
}