0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[NPOI] "A part with the name '/xl/styles.xml' already exists" エラーへの対処

Last updated at Posted at 2019-06-20

概要

NPOIワークブックオブジェクトの生成時に、以下のような例外が発生する原因と対処方法。

A part with the name '/xl/styles.xml' already exists : Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]

ケース1. XLSXファイル読み込み時にImportOptionパラメーターを指定している

エラー再現用プログラム
using System;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;

namespace NPOITest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("XLSXファイルの読み込み開始");
                using (var stream = new FileStream(@"C:\Temp\MyBook2007.xlsx", FileMode.Open))
                {
                    var book = WorkbookFactory.Create(stream, ImportOption.TextOnly);
                }
                Console.WriteLine("正常終了");
            }
            catch (Exception ex)
            {
                Console.Write("例外発生:{0}{1}{0}", Environment.NewLine, ex.ToString());
            }
            Console.ReadLine();
        }
    }
}
実行結果
XLSXファイルの読み込み開始
例外発生:
NPOI.OpenXml4Net.Exceptions.PartAlreadyExistsException: A part with the name '/xl/styles.xml' already exists : Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]
   場所 NPOI.POIXMLDocumentPart.CreateRelationship(POIXMLRelation descriptor, POIXMLFactory factory, Int32 idx, Boolean noRelation)
   場所 NPOI.XSSF.UserModel.XSSFWorkbook.OnDocumentRead()
   場所 NPOI.POIXMLDocument.Load(POIXMLFactory factory)
   場所 NPOI.XSSF.UserModel.XSSFWorkbook..ctor(OPCPackage pkg)
   場所 NPOI.SS.UserModel.WorkbookFactory.Create(Stream inputStream)
   場所 NPOI.SS.UserModel.WorkbookFactory.Create(Stream inputStream, ImportOption importOption)
   場所 NPOITest.Program.Main(String[] args) 場所 C:\Source\Repos\NPOITest\NPOITest\Program.cs:行 18
  • 原因
  • WorkbookFactory.Create()でワークブック生成する場合、第2パラメーターImportOptionを指定できるのは、ファイルがXLS形式(Excel2003以前)の場合のみ。XLSX形式(Excel2007以降)の場合は指定してはいけない。
  • 対処
  • 以下のとおり、第2パラメーターを指定しないように書き換える。
  • var book = WorkbookFactory.Create(stream);

ケース2. XLSXファイルを読み込む前にXLSファイルをImportOptionパラメーターありで読み込んでいる

エラー再現用プログラム
using System;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;

namespace NPOITest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("1. XLSファイルの読み込み開始");
                using (var stream = new FileStream(@"C:\Temp\MyBook2003.xls", FileMode.Open))
                {
                    var book2003 = WorkbookFactory.Create(stream, ImportOption.TextOnly);
                }
                Console.WriteLine("2. XLSXファイルの読み込み開始");
                using (var stream = new FileStream(@"C:\Temp\MyBook2007.xlsx", FileMode.Open))
                {
                    var book2007 = WorkbookFactory.Create(stream);
                }
                Console.WriteLine("正常終了");
            }
            catch (Exception ex)
            {
                Console.Write("例外発生:{0}{1}{0}", Environment.NewLine, ex.ToString());
            }
            Console.ReadLine();
        }
    }
}
実行結果
1. XLSファイルの読み込み開始
2. XLSXファイルの読み込み開始
例外発生:
NPOI.OpenXml4Net.Exceptions.PartAlreadyExistsException: A part with the name '/xl/styles.xml' already exists : Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]
   場所 NPOI.POIXMLDocumentPart.CreateRelationship(POIXMLRelation descriptor, POIXMLFactory factory, Int32 idx, Boolean noRelation)
   場所 NPOI.XSSF.UserModel.XSSFWorkbook.OnDocumentRead()
   場所 NPOI.POIXMLDocument.Load(POIXMLFactory factory)
   場所 NPOI.XSSF.UserModel.XSSFWorkbook..ctor(OPCPackage pkg)
   場所 NPOI.SS.UserModel.WorkbookFactory.Create(Stream inputStream)
   場所 NPOITest.Program.Main(String[] args) 場所 C:\Source\Repos\NPOITest\NPOITest\Program.cs:行 23
  • 原因
  • 2つのWorkbookFactory.Create()を個別に実行した場合はエラーにならないが、この順で実行した場合はエラーとなる。ファイルがXLS形式(Excel2003以前)であっても、その後でXLSX形式(Excel2007以降)のファイルを読み込む可能性がある場合はImportOptionを指定してはいけない。
  • 対処
  • XLSの読み込み処理をケース1と同様、第2パラメーターを指定しないように書き換える。
  • var book2003 = WorkbookFactory.Create(stream);

補足

  • NPOI version 2.4.1で確認した。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?