ExcelRTC  0.5.0
 全て クラス ファイル 関数 変数
ExcelObject.h
説明を見る。
1 // -*-C++-*-
6 #ifndef ExcelObject_H
7 #define ExcelObject_H
8 
9 using namespace Microsoft::Office::Interop::Excel;
10 #define Excel Microsoft::Office::Interop::Excel
11 
12 #include <iostream>
13 #include <string>
14 #include <vector>
15 
16 #include "SubFunction.h"
17 
18 
19 
25 ref class ExcelObject
26 {
27 public:
31  ExcelObject();
32  static ExcelObject^ Obj;
39  Excel::Worksheet^ getWorksheet(std::string sn);
40 
45  void saveRTC(std::vector<std::string> sf);
46 
51  std::vector<std::string> loadRTC();
52 
60  void resetCellColor(int c, std::string l, std::string sn, std::string leng);
61 
62 
71  void setCellStringSingle(int c, std::string l, std::string sn, std::string val);
72 
80  template <typename T>
81  void setCellValueSingle(int c, std::string l, std::string sn, T val)
82  {
83  int t_l = convertStrToVal(l);
84  Excel::Worksheet^ws = getWorksheet(sn);
85  if(ws != nullptr)
86  {
87  if(c > 0 && t_l > 0)
88  {
89  Excel::Range^c1 = safe_cast<Excel::Range^>(ws->Cells[c,t_l]);
90  c1->Value2 = val;
91  }
92  }
93  }
94 
95 
103  std::string getCellValueSingle(int c, std::string l, std::string sn)
104  {
105  int t_l = convertStrToVal(l);
106  Excel::Worksheet^ws = getWorksheet(sn);
107  if(ws != nullptr)
108  {
109  if(c > 0 && t_l > 0)
110  {
111  Excel::Range^c1 = safe_cast<Excel::Range^>(ws->Cells[c,t_l]);
112 
113  return MarshalString(c1->Value2->ToString());
114  }
115  }
116  return "error";
117  }
118 
127  template <typename T>
128  void setCellValue(int c, std::string l, std::string sn, bool state, std::vector<std::vector<T>> v)
129  {
130  Excel::Worksheet^ws = getWorksheet(sn);
131 
132 
133  if(ws != nullptr)
134  {
135  int t_l = convertStrToVal(l);
136 
137  int mnum = v.size();
138  if(!state)
139  mnum = 1;
140  for(int i=0;i < mnum;i++)
141  {
142  if(c+i > 0 && t_l > 0)
143  {
144  Excel::Range^c1 = safe_cast<Excel::Range^>(ws->Cells[c+i,t_l]);
145  Excel::Range^c2 = safe_cast<Excel::Range^>(ws->Cells[c+i,t_l+v[i].size()-1]);
146 
147 
148  Excel::Range^mr = ws->Range[c1,c2];
149 
150  array<T>^dt = gcnew array<T>(v[i].size());
151  //std::cout << v.size() << std::endl;
152  for(int j=0;j < v[i].size();j++)
153  {
154 
155  dt[j] = v[i][j];//gcnew System::String(v[i].c_str());
156 
157  //mr->Value2 = gcnew System::String(v.c_str());
158  //ws->Cells[c,l] = gcnew System::String(v.c_str());
159  }
160 
161 
162 
163  mr->Value2 = dt;
164  }
165  }
166 
167 
168  //System::Runtime::InteropServices::Marshal::ReleaseComObject(c1);
169  //System::Runtime::InteropServices::Marshal::ReleaseComObject(c2);
170  //System::Runtime::InteropServices::Marshal::ReleaseComObject(mr);
171 
172  }
173  }
174 
175 
176 
177 
186  template <typename T>
187  std::vector<T> getCellValue(int c, std::string l, std::string sn, std::string leng)
188  {
189  Excel::Worksheet^ws = getWorksheet(sn);
190 
191  std::vector<T> v;
192 
193 
194 
195  if(ws != nullptr)
196  {
197  int t_l = convertStrToVal(l);
198  int t_leng = t_l;
199 
200  try{
201  t_leng = convertStrToVal(leng);
202  }
203  catch(...)
204  {
205  t_leng = t_l;
206  }
207  if(t_l > t_leng)
208  t_leng = t_l;
209 
210 
211 
212 
213 
214  if(c > 0 && t_l > 0 && t_leng >= t_l)
215  {
216 
217 
218  if(c > 1){
219 
220 
221  Excel::Range^c1 = safe_cast<Excel::Range^>(ws->Cells[c-1,t_l]);
222  Excel::Range^c2 = safe_cast<Excel::Range^>(ws->Cells[c-1,t_leng]);
223 
224 
225  Excel::Range^mr = ws->Range[c1,c2];
226 
227  mr->Interior->ColorIndex = 0;
228  }
229 
230  {
231  Excel::Range^c1 = safe_cast<Excel::Range^>(ws->Cells[c,t_l]);
232  Excel::Range^c2 = safe_cast<Excel::Range^>(ws->Cells[c,t_leng]);
233 
234 
235  Excel::Range^mr = ws->Range[c1,c2];
236 
237  mr->Interior->ColorIndex = 6;
238  //mr->Interior->Color = System::Drawing::Color::FromArgb(red, blue, green);
239 
240  }
241  }
242 
243  //Excel::Range^c1 = safe_cast<Excel::Range^>(ws->Cells[c,l]);
244  //Excel::Range^c2 = safe_cast<Excel::Range^>(ws->Cells[c,l+leng-1]);
245 
246 
247  //Excel::Range^mr = ws->Range[c1,c2];
248 
249 
250 
251  for(int i=0;i < t_leng-t_l+1;i++)
252  {
253  if(c > 0 && t_l+i > 0)
254  {
255  Excel::Range^c1 = safe_cast<Excel::Range^>(ws->Cells[c,t_l+i]);
256  /*if(c > 1)
257  {
258  Excel::Range^c2 = safe_cast<Excel::Range^>(ws->Cells[c-1,l+i]);
259  c2->Interior->ColorIndex = 0;
260  }
261  c1->Interior->ColorIndex = 6;*/
262 
263  try{
264 
265 
266  v.push_back(string2binary<T>(MarshalString(c1->Value2->ToString()),10));
267 
268  }
269  catch(...)
270  {
271  v.push_back(0);
272  }
273  }
274  //Excel::Range^mr2 = safe_cast<Excel::Range^>(mr[0,i]);
275  //v.push_back(string2binary<T>(MarshalString(mr[0,i]->ToString()),10));
276  //v.push_back(MarshalString(mr2->Value2->ToString()));
277  //System::Runtime::InteropServices::Marshal::ReleaseComObject(mr2);
278  }
279 
280 
281  //mr->Text->ToString();
282 
283 
284  //System::Runtime::InteropServices::Marshal::ReleaseComObject(mr);
285  //System::Runtime::InteropServices::Marshal::ReleaseComObject(c1);
286  //System::Runtime::InteropServices::Marshal::ReleaseComObject(c2);
287 
288 
289  }
290 
291  return v;
292  }
293 
298  void Open(System::String^ fn);
299 
303  void Close();
304 
311  void setColor(int r, int g, int b);
312 
313  int red, green, blue;
315  System::String^ filename;
316  Excel::Application^ xlApplication;
317  Excel::Workbooks^ xlWorkbooks;
318  Excel::Workbook^ xlWorkbook;
319  Excel::Sheets^ xlWorksheets;
321  System::Collections::Generic::List<Excel::Worksheet^>^ xlWorksheet;
323 };
324 
325 #endif