テクセル

calcAuto


calcAuto.dll は、C# 等の言語から libreOffice の calc を操作する Dll です。.NET 用です。

□ 仕様

・対応 .NET .Net Framework 4.5 64ビット、32ビット

実行には、libreOffice Calc が必要です。

□ プロパティ、メソッド

□ プログラム例

Visual Studio の参照の追加で calcAuto.dll を追加します。
C#での使用例 エラー処理等は、省略しています。


// calcAuto 例
using CalcAuto;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace calcSmpl
{
   public partial class Form1 : Form
   {
      CalcAuto.LibreOffice libreOffice;
      CalcAuto.Calc libCalcd;
      CalcAuto.Sheets sheetsd;
      CalcAuto.Sheet sheetd;

      public Form1()
      {
         InitializeComponent();
      }

   // ******* form load ******* 
      private void Form1_Load(object sender, EventArgs e){

         libreOffice = new CalcAuto.LibreOffice();
      }

   // ******* 新規で起動 *******
      private void button1_Click(object sender, EventArgs e){
         
         libCalcd = libreOffice.calcOpen();           //新規
         sheetsd = libCalcd.Sheets();
         sheetd = sheetsd.getByIndex(0);              //0番目のシート選択
      }

   // ******* calcファイルを開く *******
      private void button3_Click(object sender, EventArgs e){

         string sfname = Application.StartupPath + "\\calcTest1.ods";
         libCalcd = libreOffice.calcOpen(sfname);
         sheetsd = libCalcd.Sheets();
         sheetd = sheetsd.getByName("Sheet1");         //シートSheet1選択
         libCalcd.getCurrentController().setActiveSheet(sheetd);   //Sheet1をアクティブに 前面表示
      }

   // ******* calcファイル閉じる *******
      private void button2_Click(object sender, EventArgs e){

         libCalcd.calcClose();
      }

   // ******* フォームが閉じた *******
      private void Form1_FormClosed(object sender, FormClosedEventArgs e){

         libreOffice.terminate();
      }

   // ******* cells で書込み *******
      private void button4_Click(object sender, EventArgs e){

         CalcAuto.Cells cellsd = sheetd.getCellByPosition(3, 2); //D3
         cellsd.Value = 12345.6;
         cellsd.NumberFormat = libCalcd.GetNumberFormatId("#,###.00");
         cellsd.CharHeight = 14;

         cellsd = sheetd.getCellByPosition(3, 3);                //D4
         cellsd.String = "房総半島";
         cellsd.HoriJustify = CalcAuto.CellHoriJustify.CENTER;   //位置中央
         cellsd.CellBackColor = Color.Blue.ToArgb();
         cellsd.CharColor = Color.White.ToArgb();
         cellsd.CharHeightAsian = 14;

         cellsd.getColumns().OptimalWidth = true;                 //セル幅最適化
      }

   // ******* cells で読み込み *******
      private void button5_Click(object sender, EventArgs e){

         string sv1 = sheetd.getCellByPosition(1, 2).String;     //B3
         double dv = sheetd.getCellByPosition(1, 3).Value;       //B4
         textBox1.Text = sv1 + ":" + dv.ToString();
      }

   // ******* Range で書込み *******
      private void button6_Click(object sender, EventArgs e){

         CalcAuto.Range ranged = sheetd.getCellRangeByName("B5:C6");
         ranged.Value = 567.23;
         ranged.CharHeight = 12;

         ranged = sheetd.getCellRangeByName("E2:F3");
         ranged.String = "関東平野";
         ranged.CharHeightAsian = 12;
         ranged.HoriJustify = CalcAuto.CellHoriJustify.CENTER;
         ranged.CellBackColor = Color.Green.ToArgb();
         ranged.CharColor = Color.White.ToArgb();

         ranged.getColumns().OptimalWidth = true;
      }

   // ******* Range で読み込み *******
      private void button7_Click(object sender, EventArgs e){

         string sv1 = sheetd.getCellRangeByName("B3").String;
         double dv = sheetd.getCellRangeByName("B4").Value;
         textBox2.Text = sv1 + ":" + dv.ToString();
      }

   // ******* 配列で読み込み *******
      private void button8_Click(object sender, EventArgs e){

         object[,] obd = sheetd.getCellRangeByName("B2:C3").getDataArray();
         textBox2.Text = obd[0,0].ToString() + ":" + obd[0, 1].ToString() + ":" + obd[1,0].ToString() + ":" + obd[1,1].ToString();
      }

   // ******* 配列で書き込み *******
      private void button9_Click(object sender, EventArgs e){

         object[,] obd = new object[2, 4]{
            { "品名", "ノート", "鉛筆", "消しゴム" },
            { "個数", 12, 20, 15 }
         };
         CalcAuto.Range ranged = sheetd.getCellRangeByName("B7:E8");
         ranged.setDataArray(obd);                    // 配列で書き込み

         CalcAuto.BorderLine bdrline = libreOffice.BorderLine();     //罫線
         bdrline.LineStyle = BorderLineStyle.SOLID;
         bdrline.Color = Color.Blue.ToArgb();
         bdrline.LineWidth = 35;
         ranged.TopBorder = bdrline;
         ranged.LeftBorder = bdrline;
         ranged.RightBorder = bdrline;
         ranged.BottomBorder = bdrline;
      }

   // ******* 印刷 *******
      private void button10_Click(object sender, EventArgs e){

         sheetd.SetPrintAreas("B2:E8");     //印刷範囲
         libCalcd.Print(1, "1");            //印刷部数:1 印刷ページ: 1
      }

   // ******* ファイル保存 *******
      private void button11_Click(object sender, EventArgs e){

         string sfname = Application.StartupPath + "\\Calcテスト1.ods";
         libCalcd.storeAsURL(sfname);
      }
   }
}
         

PowerShell での使用例

CalcAuto.dll については、こちらから問い合わせください。