3流プログラマのメモ書き

元開発職→社内SE→派遣で営業支援の三流プログラマのIT技術メモ書き。 このメモが忘れっぽい自分とググってきた技術者の役に立ってくれれば幸いです。(jehupc.exblog.jpから移転中)

NetからOpenOfficeCalcへの操作を汎用化したDLL(Part1)

.NetからOpenOfficeCalcへの操作を汎用化したDLLのPart1です。(C#)

using System;

using System.Collections.Generic;

using System.Text;

using uno.util;

using unoidl.com.sun.star.frame;

using unoidl.com.sun.star.lang;

using unoidl.com.sun.star.sheet;

using unoidl.com.sun.star.table;

using unoidl.com.sun.star.uno;

using unoidl.com.sun.star.beans;

using unoidl.com.sun.star.util;

using unoidl.com.sun.star.text;

using System.Management;

using System.Diagnostics;

using unoidl.com.sun.star.container;

using System.Drawing;

namespace OpenOfficeAccessRapper{

public class OpenOfficeRapper : IDisposable{

private XComponentContext context;

private XMultiServiceFactory factory;

private XComponentLoader loader;

private XSpreadsheetDocument doc;

private XSpreadsheets sheets;

private XSpreadsheet sheet;

private XCell cell;

private XCellRange xCellRange;

private string strUriFilePath;

public XCell Cell{

get { return cell; }

}

///

各方向を表す列挙型

public enum ParagraphSide{

Left, Right, Top, Bottom

}

///

コンストラクタ

/// Calcのファイル名

public OpenOffice( string calcFileName ) {

//ファイルパスを変換

Uri uriCalcFile;

Uri.TryCreate(calcFileName, UriKind.Absolute, out uriCalcFile);

strUriFilePath = uriCalcFile.ToString();

//コンポーネントコンテキストオブジェクト取得(OpenOfficeの基本プロセスらしい)

context = Bootstrap.bootstrap();

//サービスマネージャ取得

factory = (XMultiServiceFactory)context.getServiceManager();

//コンポーネントローダオブジェクト取得

loader = (XComponentLoader)factory.createInstance("com.sun.star.frame.Desktop");

//非表示で実行するためのプロパティ指定

PropertyValue[] args1 = new PropertyValue[1];

args1[0] = new PropertyValue();

args1[0].Name = "Hidden";

args1[0].Value = new uno.Any((Boolean)true);

//ファイルを開きドキュメントオブジェクトを生成

doc = (XSpreadsheetDocument)loader.loadComponentFromURL(strUriFilePath, "_blank", 0, args1);// null);

//シートたちを取得

sheets = doc.getSheets();

}

///

シートを選択する(選択したシートはメンバ変数に保持)

/// シート名

public void SelectSheet(string sheetName) {

try {

this.sheet = (XSpreadsheet)this.sheets.getByName(sheetName).Value;

} catch (System.Exception ex) {

OpenOfficeProcessKill();

throw;

}

}

///

現在アクティブなシートで指定したセルを選択。選択したセルはメンバ変数に持つ。

public void SelectCell(int iColumn, int iRow) {

try {

IsSheetNullCheckException();

this.cell = this.sheet.getCellByPosition(iColumn, iRow);

} catch (System.Exception ex) {

OpenOfficeProcessKill();

throw;

}

}

///

指定されたセル範囲を選択(選択したセル範囲はメンバ変数に保持)

public void SelectRangeByPosition(int nLeft, int nTop, int nRight, int nBottom){

try {

IsSheetNullCheckException();

xCellRange = sheet.getCellRangeByPosition(nLeft, nTop, nRight, nBottom);

} catch (System.Exception ex) {

OpenOfficeProcessKill();

throw;

}

}

///

選択したセル範囲に罫線を引く(先にSelectRangeByPositionでセル範囲指定が必要)

public void Border(int color) {

XPropertySet xPropSet = (XPropertySet)xCellRange;

BorderLine aLine = new BorderLine();

aLine.Color = color;

aLine.InnerLineWidth = aLine.LineDistance = 0;

aLine.OuterLineWidth = 10;

TableBorder aBorder = new TableBorder();

aBorder.TopLine = aBorder.BottomLine = aBorder.LeftLine = aBorder.RightLine = aLine;

aBorder.IsTopLineValid = aBorder.IsBottomLineValid = true;

aBorder.IsLeftLineValid = aBorder.IsRightLineValid = true;

aBorder.IsVerticalLineValid = true;

aBorder.IsHorizontalLineValid = true;

aBorder.HorizontalLine = aBorder.VerticalLine = aLine;

xPropSet.setPropertyValue("TableBorder", new uno.Any(typeof(unoidl.com.sun.star.table.TableBorder), aBorder));

}

///

現在選択しているセルに数値を代入(先にSelectCellでセル指定が必要)

public void SetCellValue(double value) {

try {

IsSheetNullCheckException();

IsCellNullCheckException();

this.cell.setValue(value);

} catch (System.Exception ex) {

OpenOfficeProcessKill();

throw;

}

}

///

現在選択しているセルの数値を取得(先にSelectCellでセル指定が必要)

public double GetCellValue() {

double d ;

try {

IsSheetNullCheckException();

IsCellNullCheckException();

d = this.cell.getValue();

}catch (System.Exception ex) {

OpenOfficeProcessKill();

throw;

}

return this.cell.getValue();

}

///

現在選択しているセルに数式を代入(先にSelectCellでセル指定が必要)

public void SetCellFormula(string value) {

try{

IsSheetNullCheckException();

IsCellNullCheckException();

this.cell.setFormula(value);

}catch (System.Exception ex) {

OpenOfficeProcessKill();

throw;

}

}

///

現在選択しているセルの数式を取得(先にSelectCellでセル指定が必要)

public string GetCellFormula(){

string str = string.Empty;

try{

IsSheetNullCheckException();

IsCellNullCheckException();

str=this.cell.getFormula();

}catch (System.Exception ex){

OpenOfficeProcessKill();

throw;

}

return str;

}

///

現在選択しているセルの文字列を取得(先にSelectCellでセル指定が必要)

public string GetCellString() {

string str = string.Empty;

try {

IsSheetNullCheckException();

IsCellNullCheckException();

XText txt = (XText)this.cell;

str = txt.getString();

}catch (System.Exception ex){

OpenOfficeProcessKill();

throw;

}

return str;

}

///

セル文字色を設定(先にSelectCellでセル指定が必要)

/// 設定するカラー構造体

public void SetCharColor(Color CharColor) {

Action setPropParam = SetCharColorDelegate;

PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);

prm.ColorValue = CharColor;

SetCoreProperties(setPropParam,prm);

}

///

セル文字色を取得(先にSelectCellでセル指定が必要)

/// 取得した文字色

public Color GetCharColor() {

Action getPropParam = GetCharColorDelegate;

PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);

GetCoreProperties(getPropParam, prm);

return prm.ColorValue;

}

///

セル背景色を設定(先にSelectCellでセル指定が必要)

/// 設定するカラー構造体

public void SetCellBackColor(Color CharColor){

Action setPropParam = SetCellBackColorDelegate;

PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);

prm.ColorValue = CharColor;

SetCoreProperties(setPropParam, prm);

}

続きは、.NetからOpenOfficeCalcへの操作を汎用化したDLL(Part2)へ。