MonoDevelop手动安装OpenXml 2.10.0

发布时间 2023-08-09 00:22:43作者: qydw007

MonoDevelop手动安装OpenXml 2.10.0

1 测试代码:

  • 准备在linux用MonoDevelop测试如下代码,发觉装不了OpenXml。So,手动安装。
  • 代码生成一个表格文件,其中工作表的名字为mySheet。
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

namespace xlsx_m1
{
    class Class1
    {
        static void Main(string[] args)
        {
            string filepath = "a.xlsx";
            CreateSpreadsheetWorkbook(filepath);
        }

            public static void CreateSpreadsheetWorkbook(string filepath)
        {
            // Create a spreadsheet document by supplying the filepath.
            // By default, AutoSave = true, Editable = true, and Type = xlsx.
            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);

            // Add a WorkbookPart to the document.
            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();

            // Add a WorksheetPart to the WorkbookPart.
            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            // Add Sheets to the Workbook.
            Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

            // Append a new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
            sheets.Append(sheet);

            workbookpart.Workbook.Save();

            // Close the document.
            spreadsheetDocument.Close();
        }
    }
}

2 MonoDevelop新建项目 xlsx-m1

  • 不勾选Create a project within the solution directory,免得改路径。
  • 新建类文件,如1
  • 执行3、4、5的步骤
  • 编译、运行。

3 下载包:

mkdir -pv packages/DocumentFormat.OpenXml.2.10.0
cd packages/DocumentFormat.OpenXml.2.10.0
# 下载非常慢
wget -c -O  DocumentFormat.OpenXml.2.10.0.nupkg https://www.nuget.org/api/v2/package/DocumentFormat.OpenXml/2.10.0
# 解压(其实只要lib)
unzip DocumentFormat.OpenXml.2.10.0.nupkg

4 修改 xlsx-m1.csproj 文件:

 <ItemGroup>
    <Reference Include="DocumentFormat.OpenXml">
      <HintPath>packages\DocumentFormat.OpenXml.2.10.0\lib\net40\DocumentFormat.OpenXml.dll</HintPath>
    </Reference>
    <Reference Include="WindowsBase" />
</ItemGroup>

5 修改 packages.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="DocumentFormat.OpenXml" version="2.10.0" targetFramework="net45" />
</packages>