人気ブログランキング | 話題のタグを見る
NetからOpenOfficeCalcへの操作を汎用化したDLL(Part2)
.NetからOpenOfficeCalcへの操作を汎用化したDLL(Part1)の続きで、Part2です。(C#)



/// <summary>セル背景色を取得(先にSelectCellでセル指定が必要)</summary>
/// <returns>取得した文字色</returns>
public Color GetCellBackColorr() {
Action<PropertiesDelegateParam> getPropParam = GetCellBackColorDelegate;
PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);
GetCoreProperties(getPropParam, prm);
return prm.ColorValue;
}
/// <summary>文字サイズを設定(先にSelectCellでセル指定が必要) </summary>
/// <param name="fSize">設定するサイズ</param>
public void SetCharHeight(float fSize) {
Action<PropertiesDelegateParam> setPropParam = SetCharHeightDelegate;
PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);
prm.FloatValue = fSize;
SetCoreProperties(setPropParam, prm);
}
/// <summary> 文字サイズを取得(先にSelectCellでセル指定が必要)</summary>
/// <returns>取得した文字サイズ</returns>
public float GetCharHeight() {
Action<PropertiesDelegateParam> getPropParam = GetCharHeightDelegate;
PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);
GetCoreProperties(getPropParam, prm);
return prm.FloatValue;
}
/// <summary>パディングサイズを設定(先にSelectCellでセル指定が必要) </summary>
public void SetParagraph(int iValue,ParagraphSide side) {
Action<PropertiesDelegateParam> setPropParam = SetCharHeightDelegate;
PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);
prm.SideValue = side;
SetCoreProperties(setPropParam, prm);
}
/// <summary>パディングサイズを取得(先にSelectCellでセル指定が必要) </summary>
public int GetParagraph(ParagraphSide side) {
Action<PropertiesDelegateParam> getPropParam = GetParagraphDelegate;
PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);
prm.SideValue = side;
GetCoreProperties(getPropParam, prm);
return prm.IntValue;
}
/// <summary> 背景色を透過設定(先にSelectCellでセル指定が必要) </summary>
public void SetIsCellBackgroundTransparent(bool bTrans){
Action<PropertiesDelegateParam> setPropParam = SetIsCellBackgroundTransparentDelegate;
PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);
prm.BoolValue = bTrans;
SetCoreProperties(setPropParam, prm);
}
/// <summary>文字サイズを取得(先にSelectCellでセル指定が必要)</summary>
/// <returns>取得した文字サイズ</returns>
public bool GetIsCellBackgroundTransparent() {
Action<PropertiesDelegateParam> getPropParam = GetIsCellBackgroundTransparentDelegate;
PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);
GetCoreProperties(getPropParam, prm);
return prm.BoolValue;
}

/// <summary> 行を挿入する</summary>
/// <param name="startRow">挿入開始行インデックス</param>
/// <param name="endRow">挿入する行の数</param>
public void InsertRowByIndex(int startRow, int insertRowsCount){
XColumnRowRange xCRRange = (XColumnRowRange)sheet;
XTableRows xRows = xCRRange.getRows();
xRows.insertByIndex(startRow, insertRowsCount);
}
/// <summary>指定した行を削除する </summary>
/// <param name="startRow">削除開始行インデックス</param>
/// <param name="endRos">削除する行の数</param>
public void RemoveRowByIndex(int startRow, int removeRowsCount){
XColumnRowRange xCRRange = (XColumnRowRange)sheet;
XTableRows xRows = xCRRange.getRows();
xRows.removeByIndex(startRow, removeRowsCount);
}

/// <summary> OpenOfficeのファイルを保存 </summary>
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;
}
}

/// <summary>OpenOfficeのファイルを上書保存し、閉じる(プロセスも殺す) </summary>
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();
}
}

/// <summary>OpenOfficeを閉じる(プロセスは殺さない) </summary>
public void OpenOfficeClose(){
if (doc != null){
XCloseable xCloseable = (XCloseable)doc;
xCloseable.close(true);
doc = null;
factory = null;
loader = null;
context = null;
}
}

/// <summary> OpenOfficeのプロセス強制終了をする。 </summary>
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 {
//プロセス強制終了時のエラーは無視
}
}

/// <summary>シートが選択されているかどうか</summary>
/// <returns></returns>
public bool IsSheetNullCheck(){
return (this.sheet == null ? false: true);
}
/// <summary> セルが選択されているかどうか</summary>
/// <returns></returns>
public bool IsCellNullCheck(){
return (this.cell == null ? false : true);
}

#region 汎用メソッド
/// <summary> プロパティをセットする汎用メソッド(共通部分のみ記述。実際の処理はデリゲートに任せる) </summary>
/// <param name="setPropParam">デリゲート変数</param>
/// <param name="prm">プロパティに設定する値クラス(各型を持っただけのエンティティクラス)</param>
private void SetCoreProperties(Action<PropertiesDelegateParam> setPropParam, PropertiesDelegateParam prm){
try {
IsSheetNullCheckException();
IsCellNullCheckException();
//デリゲート呼び出し
setPropParam(prm);
} catch (System.Exception ex) {
OpenOfficeProcessKill();
throw;
}
}
/// <summary> プロパティを取得する汎用メソッド(共通部分のみ記述。実際の処理はデリゲートに任せる) </summary>
/// <param name="getPropParam">デリゲート変数</param>
/// <param name="prm">プロパティに取得値をもつ値クラス(各型を持っただけのエンティティクラス)</param>
private void GetCoreProperties(Action<PropertiesDelegateParam> getPropParam, PropertiesDelegateParam prm){
try{
IsSheetNullCheckException();
IsCellNullCheckException();
//デリゲート呼び出し
getPropParam(prm);
}catch (System.Exception ex){
OpenOfficeProcessKill();
throw;
}
}
#endregion


続きは、.NetからOpenOfficeCalcへの操作を汎用化したDLL(Part3)へ。
by Jehoshaphat | 2011-10-20 02:27 | .Net開発


<< .NetからOpenOffic... NetからOpenOffice... >>