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

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

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

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

///

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

/// 取得した文字色

public Color GetCellBackColorr() {

Action getPropParam = GetCellBackColorDelegate;

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

GetCoreProperties(getPropParam, prm);

return prm.ColorValue;

}

///

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

/// 設定するサイズ

public void SetCharHeight(float fSize) {

Action setPropParam = SetCharHeightDelegate;

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

prm.FloatValue = fSize;

SetCoreProperties(setPropParam, prm);

}

///

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

/// 取得した文字サイズ

public float GetCharHeight() {

Action getPropParam = GetCharHeightDelegate;

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

GetCoreProperties(getPropParam, prm);

return prm.FloatValue;

}

///

パディングサイズを設定(先にSelectCellでセル指定が必要)

public void SetParagraph(int iValue,ParagraphSide side) {

Action setPropParam = SetCharHeightDelegate;

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

prm.SideValue = side;

SetCoreProperties(setPropParam, prm);

}

///

パディングサイズを取得(先にSelectCellでセル指定が必要)

public int GetParagraph(ParagraphSide side) {

Action getPropParam = GetParagraphDelegate;

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

prm.SideValue = side;

GetCoreProperties(getPropParam, prm);

return prm.IntValue;

}

///

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

public void SetIsCellBackgroundTransparent(bool bTrans){

Action setPropParam = SetIsCellBackgroundTransparentDelegate;

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

prm.BoolValue = bTrans;

SetCoreProperties(setPropParam, prm);

}

///

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

/// 取得した文字サイズ

public bool GetIsCellBackgroundTransparent() {

Action getPropParam = GetIsCellBackgroundTransparentDelegate;

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

GetCoreProperties(getPropParam, prm);

return prm.BoolValue;

}

///

行を挿入する

/// 挿入開始行インデックス

/// 挿入する行の数

public void InsertRowByIndex(int startRow, int insertRowsCount){

XColumnRowRange xCRRange = (XColumnRowRange)sheet;

XTableRows xRows = xCRRange.getRows();

xRows.insertByIndex(startRow, insertRowsCount);

}

///

指定した行を削除する

/// 削除開始行インデックス

/// 削除する行の数

public void RemoveRowByIndex(int startRow, int removeRowsCount){

XColumnRowRange xCRRange = (XColumnRowRange)sheet;

XTableRows xRows = xCRRange.getRows();

xRows.removeByIndex(startRow, removeRowsCount);

}

///

OpenOfficeのファイルを保存

public void OpenOfficeSave() {

try{

//保存するために使うクラス(インターフェイス?)取得

XStorable xstorable = (XStorable)doc;

//保存時のプロパティ設定

PropertyValue[] storeProps = new PropertyValue[1];

storeProps[0] = new PropertyValue();

storeProps[0].Name = "Overwrite"; //上書き

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

String sURL = strUriFilePath;

//保存

xstorable.storeAsURL(sURL, storeProps);

}catch (System.Exception ex){

OpenOfficeProcessKill();

throw;

}

}

///

OpenOfficeのファイルを上書保存し、閉じる(プロセスも殺す)

public void OpenOfficeSaveAndClose(){

//保存するために使うクラス(インターフェイス?)取得

XStorable xstorable = (XStorable)doc;

//保存時のプロパティ設定

PropertyValue[] storeProps = new PropertyValue[1];

storeProps[0] = new PropertyValue();

storeProps[0].Name = "Overwrite"; //上書き

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

try{

String sURL = strUriFilePath;

//保存

xstorable.storeAsURL(sURL, storeProps);

//閉じる

OpenOfficeClose();

}catch (unoidl.com.sun.star.uno.Exception ex){

throw;

}finally{

//OpenOfficeのプロセス強制終了

OpenOfficeProcessKill();

}

}

///

OpenOfficeを閉じる(プロセスは殺さない)

public void OpenOfficeClose(){

if (doc != null){

XCloseable xCloseable = (XCloseable)doc;

xCloseable.close(true);

doc = null;

factory = null;

loader = null;

context = null;

}

}

///

OpenOfficeのプロセス強制終了をする。

public static void OpenOfficeProcessKill() {

try{

//現在のユーザ名取得

string strUserName = Environment.UserName;

//open office のプロセス検索。

Process[] ps = Process.GetProcessesByName("soffice.bin");

//WMIからOpenOfficeのプロセス取得

ManagementObjectSearcher query = new ManagementObjectSearcher(@"SELECT * FROM Win32_Process Where Name = 'soffice.bin'");

ManagementObjectCollection col = query.Get();

//配列から1つずつ取り出す

foreach (System.Diagnostics.Process p in ps){

//現在のユーザ名とプロセスの実行ユーザ名が同じなら殺す

foreach (ManagementObject o in col){

int pId = int.Parse(o["ProcessId"].ToString());

Object[] UserInfo = new object[2];

o.InvokeMethod("GetOwner", UserInfo);

if (p.Id == pId && strUserName.Equals((string)UserInfo[0])){

p.Kill();

break;

}

}

}

} catch {

//プロセス強制終了時のエラーは無視

}

}

///

シートが選択されているかどうか

///

public bool IsSheetNullCheck(){

return (this.sheet == null ? false: true);

}

///

セルが選択されているかどうか

///

public bool IsCellNullCheck(){

return (this.cell == null ? false : true);

}

#region 汎用メソッド

///

プロパティをセットする汎用メソッド(共通部分のみ記述。実際の処理はデリゲートに任せる)

/// デリゲート変数

/// プロパティに設定する値クラス(各型を持っただけのエンティティクラス)

private void SetCoreProperties(Action setPropParam, PropertiesDelegateParam prm){

try {

IsSheetNullCheckException();

IsCellNullCheckException();

//デリゲート呼び出し

setPropParam(prm);

} catch (System.Exception ex) {

OpenOfficeProcessKill();

throw;

}

}

///

プロパティを取得する汎用メソッド(共通部分のみ記述。実際の処理はデリゲートに任せる)

/// デリゲート変数

/// プロパティに取得値をもつ値クラス(各型を持っただけのエンティティクラス)

private void GetCoreProperties(Action getPropParam, PropertiesDelegateParam prm){

try{

IsSheetNullCheckException();

IsCellNullCheckException();

//デリゲート呼び出し

getPropParam(prm);

}catch (System.Exception ex){

OpenOfficeProcessKill();

throw;

}

}

#endregion

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