人気ブログランキング | 話題のタグを見る
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; }
}
/// <summary>各方向を表す列挙型</summary>
public enum ParagraphSide{
Left, Right, Top, Bottom
}

/// <summary>コンストラクタ</summary>
/// <param name="calcFileName">Calcのファイル名</param>
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();
}

/// <summary>シートを選択する(選択したシートはメンバ変数に保持) </summary>
/// <param name="sheetName">シート名</param>
public void SelectSheet(string sheetName) {
try {
this.sheet = (XSpreadsheet)this.sheets.getByName(sheetName).Value;
} catch (System.Exception ex) {
OpenOfficeProcessKill();
throw;
}
}

/// <summary> 現在アクティブなシートで指定したセルを選択。選択したセルはメンバ変数に持つ。</summary>
public void SelectCell(int iColumn, int iRow) {
try {
IsSheetNullCheckException();
this.cell = this.sheet.getCellByPosition(iColumn, iRow);
} catch (System.Exception ex) {
OpenOfficeProcessKill();
throw;
}
}

/// <summary>指定されたセル範囲を選択(選択したセル範囲はメンバ変数に保持)</summary>
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;
}
}

/// <summary>選択したセル範囲に罫線を引く(先にSelectRangeByPositionでセル範囲指定が必要) </summary>
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));
}

/// <summary>現在選択しているセルに数値を代入(先にSelectCellでセル指定が必要) </summary>
public void SetCellValue(double value) {
try {
IsSheetNullCheckException();
IsCellNullCheckException();
this.cell.setValue(value);
} catch (System.Exception ex) {
OpenOfficeProcessKill();
throw;
}
}

/// <summary>現在選択しているセルの数値を取得(先にSelectCellでセル指定が必要)</summary>
public double GetCellValue() {
double d ;
try {
IsSheetNullCheckException();
IsCellNullCheckException();
d = this.cell.getValue();
}catch (System.Exception ex) {
OpenOfficeProcessKill();
throw;
}
return this.cell.getValue();
}

/// <summary>現在選択しているセルに数式を代入(先にSelectCellでセル指定が必要)</summary>
public void SetCellFormula(string value) {
try{
IsSheetNullCheckException();
IsCellNullCheckException();
this.cell.setFormula(value);
}catch (System.Exception ex) {
OpenOfficeProcessKill();
throw;
}
}

/// <summary>現在選択しているセルの数式を取得(先にSelectCellでセル指定が必要) </summary>
public string GetCellFormula(){
string str = string.Empty;
try{
IsSheetNullCheckException();
IsCellNullCheckException();
str=this.cell.getFormula();
}catch (System.Exception ex){
OpenOfficeProcessKill();
throw;
}
return str;
}


/// <summary> 現在選択しているセルの文字列を取得(先にSelectCellでセル指定が必要)</summary>
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;
}

/// <summary>セル文字色を設定(先にSelectCellでセル指定が必要) </summary>
/// <param name="CharColor">設定するカラー構造体</param>
public void SetCharColor(Color CharColor) {
Action<PropertiesDelegateParam> setPropParam = SetCharColorDelegate;
PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);
prm.ColorValue = CharColor;
SetCoreProperties(setPropParam,prm);
}
/// <summary>セル文字色を取得(先にSelectCellでセル指定が必要) </summary>
/// <returns>取得した文字色</returns>
public Color GetCharColor() {
Action<PropertiesDelegateParam> getPropParam = GetCharColorDelegate;
PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);
GetCoreProperties(getPropParam, prm);
return prm.ColorValue;
}
/// <summary> セル背景色を設定(先にSelectCellでセル指定が必要)</summary>
/// <param name="CharColor">設定するカラー構造体</param>
public void SetCellBackColor(Color CharColor){
Action<PropertiesDelegateParam> setPropParam = SetCellBackColorDelegate;
PropertiesDelegateParam prm = new PropertiesDelegateParam((XPropertySet)this.cell);
prm.ColorValue = CharColor;
SetCoreProperties(setPropParam, prm);
}



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


<< NetからOpenOffic... (.Net,OpenOffic... >>