質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
LibreOffice Calc

LibreOffice Calcは、様々なOSで利用可能なオフィススイートであるLibreOfficeに含まれるオープンソースの表計算ソフトです。テンプレートや背景色、枠線といった書式設定の各項目を柔軟で設定することができます。

Visual C++

Microsoft Visual C++はWindowsのCとC++の統合開発環境(IDE)であり、コンパイラやデバッガを含んでいます。

Q&A

解決済

1回答

1672閲覧

【C++】文字列の指定の仕方が分からない

Helloc

総合スコア16

LibreOffice Calc

LibreOffice Calcは、様々なOSで利用可能なオフィススイートであるLibreOfficeに含まれるオープンソースの表計算ソフトです。テンプレートや背景色、枠線といった書式設定の各項目を柔軟で設定することができます。

Visual C++

Microsoft Visual C++はWindowsのCとC++の統合開発環境(IDE)であり、コンパイラやデバッガを含んでいます。

0グッド

0クリップ

投稿2020/08/29 04:44

編集2020/08/29 08:22

C++で LibreOficeを操作したいのですが

エラー C2440 '<function-style-cast>': 'char [2048]' から 'rtl::OUString' に変換できません。
エラー (アクティブ) E0289 コンストラクター "rtl::OUString::OUString" のインスタンスが引数リストと一致しません

とエラーで コンパイルが通りません

Reference<XComponent> xComponent(xComponentLoader->loadComponentFromURL(
OUString(foo), // ここを OUString("file:///F:/New%20Book.xlsx"), なら OK
OUString("_blank"), 0,
Sequence < ::com::sun:⭐:beans::PropertyValue >()));

OUString(foo),

OUString("file:///F:/New%20Book.xlsx"),
にすると
ビルドでき、実行が可能なのですが

ファイルを開くダイアログで 取得した ファイル名を
渡すには どのように 記述すれば 良いのでしょうか?

ご教示 お願い致します

C++

1#pragma comment(lib, "Ws2_32.lib") 2#pragma comment(lib, "isal.lib") 3#pragma comment(lib, "icppu.lib") 4#pragma comment(lib, "icppuhelper.lib") 5 6// Simple client application using the UnoUrlResolver service. 7#include <iostream> 8 9#include <cppuhelper/bootstrap.hxx> 10#include <com/sun/star/bridge/XUnoUrlResolver.hpp> 11#include <com/sun/star/frame/Desktop.hpp> 12#include <com/sun/star/registry/XSimpleRegistry.hpp> 13#include <com/sun/star/sheet/XSpreadsheet.hpp> 14#include <com/sun/star/sheet/XSpreadsheetDocument.hpp> 15 16 17 18#include <Ole2.h> 19#include <stdio.h> 20#include <atlbase.h> 21#include <atlstr.h> 22#include <atlcom.h> 23 24 25 26#include <cstdio> 27#include <stdlib.h> 28#include <psapi.h> 29#pragma comment(lib, "psapi.lib") 30 31 32#include <windows.h> 33#include <tchar.h> 34 35#include <stdio.h> 36 37 38#include <cstdio> 39#include <stdio.h> 40#include <string.h> 41#include <cstringt.h> 42#include <string> 43#include <iostream> 44#include <vector> 45#include <atlstr.h> 46#include <wchar.h> 47#include <stdlib.h> 48#include <stdio.h> 49#include <algorithm> 50 51 52#include <assert.h> 53 54 55using namespace std; 56 57 58using namespace com::sun::star::uno; 59using namespace com::sun::star::lang; 60using namespace com::sun::star::frame; 61using namespace com::sun::star::sheet; 62using namespace com::sun::star::table; 63 64using ::rtl::OUString; 65 66CString OpenDiaog(HWND hWnd, LPCSTR Filter, char* FileName, DWORD Flags); 67 68int insertIntoCell(int CellX, int CellY, 69 const std::string& value, 70 Reference<XSpreadsheet> sheet) 71{ 72 //try { 73 Reference<XCell> xCell = nullptr; 74 xCell = sheet->getCellByPosition(CellX, CellY); 75 xCell->setFormula(OUString::createFromAscii(value.c_str())); 76 //} 77 /*catch (const com::sun::star::lang::IndexOutOfBoundsException& ex) { 78 std::cerr << "Could not get Cell"; 79 return 1; 80 }*/ 81 82 return 0; 83} 84 85std::string ReplaceString 86( 87 std::string String1 // 置き換え対象 88 , std::string String2 // 検索対象 89 , std::string String3 // 置き換える内容 90) 91{ 92 std::string::size_type Pos(String1.find(String2)); 93 94 while (Pos != std::string::npos) 95 { 96 String1.replace(Pos, String2.length(), String3); 97 Pos = String1.find(String2, Pos + String3.length()); 98 } 99 100 return String1; 101} 102int insertIntoCell(int CellX, int CellY, 103 double value, 104 Reference<XSpreadsheet> sheet) 105{ 106 //try { 107 Reference<XCell> xCell = nullptr; 108 xCell = sheet->getCellByPosition(CellX, CellY); 109 xCell->setValue(value); 110 //} 111 /* 112 catch (const com::sun::star::lang::IndexOutOfBoundsException& ex) { 113 std::cerr << "Could not get Cell"; 114 return 1; 115 } 116 */ 117 return 0; 118} 119 120CString ConvertToUrl(CString sFileName) 121{ 122 if (sFileName.Left(7) == "file://") { 123 return sFileName; 124 } 125 std:string sTmpFile = sFileName.GetBuffer(); 126 sTmpFile = ReplaceString(sTmpFile, "\", "/"); 127 sTmpFile = ReplaceString(sTmpFile, "%", "%25"); 128 sTmpFile = ReplaceString(sTmpFile, " ", "%20"); 129 sTmpFile = "file:///" + sTmpFile; 130 131 MessageBoxA(NULL, sTmpFile.c_str(), "Test", MB_OK); 132 133 return (CString)(sTmpFile.c_str()); 134 135 136} 137 138int start_libreoffice_calc() 139{ 140 try 141 { 142 // Get the remote office component context 143 Reference<XComponentContext> xContext(::cppu::bootstrap()); 144 if (!xContext.is()) 145 { 146 std::cerr << "No component context!\n"; 147 return 1; 148 } 149 150 // get the remote office service manager 151 Reference<XMultiComponentFactory> xServiceManager( 152 xContext->getServiceManager()); 153 if (!xServiceManager.is()) 154 { 155 std::cerr << "No service manager!\n"; 156 return 1; 157 } 158 159 // get an instance of the remote office desktop UNO service 160 // and query the XComponentLoader interface 161 Reference<XDesktop2> xComponentLoader = Desktop::create(xContext); 162 163 // open a spreadsheet document 164 165 CString FilePath = ConvertToUrl(OpenDiaog(0, NULL, NULL,0)); 166 //int len = _tcsclen(FilePath) + 1; 167 char foo[2048]; 168 strcpy_s(foo,FilePath); 169 170 171 172 int res = (strcmp("file:///F:/New%20Book.xlsx", foo)); 173 string ret = std::to_string(res); 174 MessageBox(NULL, ret.c_str(), "Result", MB_OK); 175 176 177 178 MessageBox(NULL,foo,"Test",MB_OK); 179 Reference<XComponent> xComponent(xComponentLoader->loadComponentFromURL( 180 OUString(foo), // ここを OUString("file:///F:/New%20Book.xlsx"), なら OK 181 //OUString("file:///F:/New%20Book.xlsx"), 182 OUString("_blank"), 0, 183 Sequence < ::com::sun::star::beans::PropertyValue >())); 184 if (!xComponent.is()) 185 { 186 std::cerr << "opening spreadsheet document failed!\n"; 187 return 1; 188 } 189 190 // Reference to the document 191 Reference <XSpreadsheetDocument> spreadDoc(xComponent, UNO_QUERY); 192 193 // Get the first sheet 194 Reference<XSpreadsheets> rSheets = spreadDoc->getSheets(); 195 Any rSheet = rSheets->getByName(OUString::createFromAscii("Sheet1")); 196 Reference<XSpreadsheet> sheet(rSheet, UNO_QUERY); 197 198 // Insert cells 199 insertIntoCell(1, 1, "I was inserted from C++", sheet); 200 insertIntoCell(1, 2, 3.14, sheet); 201 insertIntoCell(0, 3, "Formula pi^2:", sheet); 202 insertIntoCell(1, 3, "=B3*B3", sheet); 203 204 } 205 catch (::cppu::BootstrapException& e) 206 { 207 std::cerr << "Caught BootstrapException: " 208 << OUStringToOString(e.getMessage(), RTL_TEXTENCODING_ASCII_US).getStr() 209 << std::endl; 210 return 1; 211 } 212 catch (Exception& e) 213 { 214 std::cerr << "Caught UNO exception: " 215 << OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr() 216 << std::endl; 217 return 1; 218 } 219 220 return 0; 221} 222 223 224int main() 225{ 226 start_libreoffice_calc(); 227 228 return 0; 229} 230CString OpenDiaog(HWND hWnd, LPCSTR Filter, char* FileName, DWORD Flags) 231{ 232 static OPENFILENAME ofn; 233 static TCHAR szPath[MAX_PATH]; 234 static TCHAR szFile[MAX_PATH]; 235 236 if (szPath[0] == TEXT('\0')) { 237 GetCurrentDirectory(MAX_PATH, szPath); 238 } 239 if (ofn.lStructSize == 0) { 240 ofn.lStructSize = sizeof(OPENFILENAME); 241 ofn.hwndOwner = hWnd; 242 ofn.lpstrInitialDir = szPath; // 初期フォルダ位置 243 ofn.lpstrFile = szFile; // 選択ファイル格納 244 ofn.nMaxFile = MAX_PATH; 245 ofn.lpstrFilter = TEXT("Excelファイル(*.xls;*.xlsx;*.xlsm)\0*.xls;*.xlsx;*.xlsm\0"); 246 ofn.lpstrTitle = TEXT("Excelファイルを選択します"); 247 ofn.Flags = OFN_FILEMUSTEXIST; 248 } 249 if (::GetOpenFileName(&ofn)) { 250 //MessageBox(hWnd, szFile, TEXT("ファイルを開く"), MB_OK); 251 CString ret = szFile; 252 return ret; 253 } 254 return ""; 255}

KoichiSugiyamaさんに アドバイスいただいた通り
下記修正で 解決しました

CString FilePath = ConvertToUrl(OpenDiaog(0, NULL, NULL,0));
//int len = _tcsclen(FilePath) + 1;
//char foo[2048];
//strcpy_s(foo,FilePath);
CStringW wideStr = (CStringW)FilePath;
wchar_t foo[2048];
wcscpy_s(foo, sizeof(foo) / sizeof(foo[0]), wideStr);

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

直接の回答ではないのですが、
LibreOffice SDK 6.4のリファレンス
を見たところ、rtl::OUStringのコンストラクタにchar文字列を引数にしたものが見当たりませんでした。おそらくunicode文字列ならうまくいくのではないかと思います。

c++

1char foo[2048];

c++

1wchar_t foo[2048];

としてやって、以降文字列コピーもwcscpy_sを使うなど、unicode文字列を意識して書き換えてみてはどうでしょうか。

投稿2020/08/29 07:24

KoichiSugiyama

総合スコア3041

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Helloc

2020/08/29 08:23

解決しました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問