Пример #1
0
        public void AddPivotCache()
        {
            XSSFWorkbook wb = new XSSFWorkbook();

            try
            {
                CT_Workbook   ctWb       = wb.GetCTWorkbook();
                CT_PivotCache pivotCache = wb.AddPivotCache("0");
                //Ensures that pivotCaches is Initiated
                Assert.IsTrue(ctWb.IsSetPivotCaches());
                Assert.AreSame(pivotCache, ctWb.pivotCaches.GetPivotCacheArray(0));
                Assert.AreEqual("0", pivotCache.id);
            }
            finally
            {
                wb.Close();
            }
        }
Пример #2
0
        public void ReadExcel(ref FaceObj _FaceObj, System.IO.FileInfo[] files)
        {
            try
            {
                #region
                //遍历文件
                for (int i = 0; i < files.Length; i++)
                {
                    string strPathName = (files[i].DirectoryName + "\\" + files[i].Name);
                    if (!File.Exists(strPathName))
                    {
                        //MyMessage.showYes("文件不存在!");
                        MyMessage.showYes("文件不存在");
                        return;
                    }

                    try//捕获Excel异常
                    {
                        using (var far = new FileStream(strPathName, FileMode.Open, FileAccess.Read))
                        {
                            XSSFWorkbook xBook = new XSSFWorkbook(far);
                            int          x     = xBook.GetCTWorkbook().sheets.sheet.Count;
                            for (int o = 0; o < x; o++)
                            {
                                //ReadSheetFromExcel(xBook, o, ref _FaceObj);
                                far.Close();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _WriteLog.WriteToLog("ReadExcel", ex);
                        return;
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                _WriteLog.WriteToLog("ReadExcel", ex);
                //System.Windows.MessageBox.Show(ex.Message);
            }
        }
Пример #3
0
        public void SaveLoadNew()
        {
            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);

            pkg.Close();
        }