2024年11月5日 星期二

[研究]SpreadsheetLight.3.5.0 試用 (支援 Excel和Calc,但支援.xlsx不支援.ods)

[研究]SpreadsheetLight.3.5.0 試用 (支援 Excel和Calc,但支援.xlsx不支援.ods)

2024-11-05

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19

https://spreadsheetlight.com/
Open source developer-friendly spreadsheet library compatible with Microsoft Excel 2007/2010/2013 and LibreOffice Calc.
實際找查,支援 Calc,但整個網站沒找到 .ods,可能只支援 Excel檔案。

https://spreadsheetlight.com/license/
SpreadsheetLight uses the MIT License. 

https://spreadsheetlight.com/sample-code/

********************************************************************************

相依

  • System.Drawing.Common.5.0.0
  • System.IO.FileSystem.Primitives.4.3.0
  • System.IO.Packaging.4.7.0
  • DocumentFormat.OpenXml.2.11.3
  • SpreadsheetLight.3.5.0

********************************************************************************


  • DocumentFormat.OpenXml.2.11.3 -> DocumentFormat.OpenXml.3.1.1
  • System.IO.Packaging.4.7.0 -> System.IO.Packaging.8.0.1
  • System.Drawing.Common.5.0.0 -> System.Drawing.Common.8.0.10
  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1 -> Microsoft.CodeDom.Providers.DotNetCompilerPlatform.4.1.0

正在安裝:

  • DocumentFormat.OpenXml.Framework.3.1.1

********************************************************************************

參考

https://spreadsheetlight.com/downloads/samplecode/HelloWorld.cs

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>


Default.aspx.cs

using SpreadsheetLight;
using System;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // https://spreadsheetlight.com/downloads/samplecode/HelloWorld.cs

            SLDocument sl = new SLDocument();

            // set a boolean at "A1"
            sl.SetCellValue("A1", true);

            // set at row 2, columns 1 through 20, a value that's equal to the column index
            for (int i = 1; i <= 20; ++i) sl.SetCellValue(2, i, i);

            // set the value of PI
            sl.SetCellValue("B3", 3.14159);

            // set the value of PI at row 4, column 2 (or "B4") in string form.
            // use this when you already have numeric data in string form and don't
            // want to parse it to a double or float variable type
            // and then set it as a value.
            // Note that "3,14159" is invalid. Excel (or Open XML) stores numerals in
            // invariant culture mode. Frankly, even "1,234,567.89" is invalid because
            // of the comma. If you can assign it in code, then it's fine, like so:
            // double fTemp = 1234567.89;
            sl.SetCellValueNumeric(4, 2, "3.14159");

            // normal string data
            sl.SetCellValue("C6", "This is at C6!");

            // typical XML-invalid characters are taken care of,
            // in particular the & and < and >
            sl.SetCellValue("I6", "Dinner & Dance costs < $10");

            // this sets a cell formula
            // Note that if you want to set a string that starts with the equal sign,
            // but is not a formula, prepend a single quote.
            // For example, "'==" will display 2 equal signs
            sl.SetCellValue(7, 3, "=SUM(A2:T2)");

            // if you need cell references and cell ranges *really* badly, consider the SLConvert class.
            sl.SetCellValue(SLConvert.ToCellReference(7, 4), string.Format("=SUM({0})", SLConvert.ToCellRange(2, 1, 2, 20)));

            // dates need the format code to be displayed as the typical date.
            // Otherwise it just looks like a floating point number.
            sl.SetCellValue("C8", new DateTime(3141, 5, 9));
            SLStyle style = sl.CreateStyle();
            style.FormatCode = "d-mmm-yyyy";
            sl.SetCellStyle("C8", style);

            sl.SetCellValue(8, 6, "I predict this to be a significant date. Why, I do not know...");

            sl.SetCellValue(9, 4, 456.123789);
            // we don't have to create a new SLStyle because
            // we only used the FormatCode property
            style.FormatCode = "0.000%";
            sl.SetCellStyle(9, 4, style);

            sl.SetCellValue(9, 6, "Perhaps a phenomenal growth in something?");

            sl.SaveAs("HelloWorld.xlsx");

            Console.WriteLine("End of program");
            Console.ReadLine();
        }
    }
}



********************************************************************************

結果

受控偵錯助理 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0x597faa96, on thread 0x23ac. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.'

********************************************************************************
改用 .NET WinForm Console 程式測試





表示無法支援 ASP.NET 架構。

(完)

相關

沒有留言:

張貼留言