Commit 5c7c945f authored by Sergey.Konovalov's avatar Sergey.Konovalov Committed by Alexander Trofimov

обьединение DocxFile2 и XlsxSerializerCom

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@58233 954022d7-b5bf-4e40-9824-e11837661b57
parent 43ef04f8
......@@ -3,6 +3,7 @@
#include "resource.h"
#include "DocxFile2.h"
#include "XlsxFile2.h"
// The module attribute causes DllMain, DllRegisterServer and DllUnregisterServer to be automatically implemented for you
[ module(dll, uuid = "{A1EEE61A-FAA7-47af-B078-4E955623B9CA}",
......
......@@ -142,6 +142,7 @@ IDB_DEFAULT_DOC_APP DOCXWR "Resources\\app.xml"
IDB_DEFAULT_DOC_CORE DOCXWR "Resources\\core.xml"
IDB_DEFAULT_DOC_THEME DOCXWR "Resources\\theme.xml"
IDB_DEFAULT_DOC_WEBSETTINGS DOCXWR "Resources\\webSettings.xml"
IDB_DEFAULT_XLSX_THEME XLSXSER "..\\XlsxSerializerCom\\Resource\\theme1.xml"
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
......@@ -549,6 +549,14 @@
<Filter
Name="XLSX"
>
<File
RelativePath=".\DocWrapper\XlsxSerializer.cpp"
>
</File>
<File
RelativePath=".\DocWrapper\XlsxSerializer.h"
>
</File>
<Filter
Name="Common"
>
......@@ -576,6 +584,10 @@
RelativePath="..\XlsxSerializerCom\Writer\BinaryReader.h"
>
</File>
<File
RelativePath="..\XlsxSerializerCom\Reader\CSVReader.cpp"
>
</File>
<File
RelativePath="..\XlsxSerializerCom\Reader\CSVReader.h"
>
......@@ -608,6 +620,10 @@
RelativePath="..\XlsxSerializerCom\Reader\CommonWriter.h"
>
</File>
<File
RelativePath="..\XlsxSerializerCom\Writer\CSVWriter.cpp"
>
</File>
<File
RelativePath="..\XlsxSerializerCom\Writer\CSVWriter.h"
>
......@@ -722,6 +738,10 @@
RelativePath=".\stdafx.h"
>
</File>
<File
RelativePath=".\XlsxFile2.h"
>
</File>
</Files>
<Globals>
</Globals>
......
#ifndef BIN_READER_WRITER_DEFINES
#define BIN_READER_WRITER_DEFINES
#ifndef DOCX_BIN_READER_WRITER_DEFINES
#define DOCX_BIN_READER_WRITER_DEFINES
namespace BinDocxRW
{
......@@ -804,4 +804,4 @@ extern int g_nCurFormatVersion;
};}
}
#endif // #ifndef BIN_READER_WRITER_DEFINES
\ No newline at end of file
#endif // #ifndef DOCX_BIN_READER_WRITER_DEFINES
\ No newline at end of file
......@@ -18,8 +18,6 @@
int BinDocxRW::g_nCurFormatVersion = 0;
#define BUFFER_GROW_SIZE 1 * 1024 * 1024 //1mb
BinDocxRW::CDocxSerializer::CDocxSerializer()
{
m_oBinaryFileWriter = NULL;
......@@ -253,7 +251,7 @@ bool BinDocxRW::CDocxSerializer::loadFromFile(std::wstring& sSrcFileName, std::w
}
return bResultOk;
}
bool BinDocxRW::CDocxSerializer::GetXmlContent(unsigned char* pBinaryObj, long lSize, long lStart, long lLength, std::wstring& sOutputXml)
bool BinDocxRW::CDocxSerializer::getXmlContent(unsigned char* pBinaryObj, long lSize, long lStart, long lLength, std::wstring& sOutputXml)
{
Streams::CBuffer oBuffer;
Streams::CBufferedStream oBufferedStream;
......@@ -270,7 +268,7 @@ bool BinDocxRW::CDocxSerializer::GetXmlContent(unsigned char* pBinaryObj, long l
sOutputXml = oTempContentWriter.m_oContent.GetData().GetString();
return true;
}
bool BinDocxRW::CDocxSerializer::GetBinaryContent(std::wstring& bsTxContent, unsigned char** ppBinary, long &lDataSize)
bool BinDocxRW::CDocxSerializer::getBinaryContent(std::wstring& bsTxContent, unsigned char** ppBinary, long &lDataSize)
{
if(NULL == m_oBinaryFileWriter)
return false;
......
......@@ -28,8 +28,8 @@ namespace BinDocxRW {
CDocxSerializer();
bool loadFromFile(std::wstring& sSrcFileName, std::wstring& sDstPath, std::wstring& sXMLOptions, std::wstring& sThemePath, std::wstring& sMediaPath);
bool saveToFile(std::wstring& sSrcFileName, std::wstring& sDstPath, std::wstring& sXMLOptions);
bool GetXmlContent(unsigned char* pBinaryObj, long lSize, long lStart, long lLength, std::wstring& sOutputXml);
bool GetBinaryContent(std::wstring& bsTxContent, unsigned char** ppBinary, long& lDataSize);
bool getXmlContent(unsigned char* pBinaryObj, long lSize, long lStart, long lLength, std::wstring& sOutputXml);
bool getBinaryContent(std::wstring& bsTxContent, unsigned char** ppBinary, long& lDataSize);
void setFontDir(std::wstring& sFontDir);
void setEmbeddedFontsDir(std::wstring& sEmbeddedFontsDir);
void setIsNoBase64Save(bool bIsNoBase64Save);
......
#include "XlsxSerializer.h"
#include "../../DesktopEditor/common/Directory.h"
#include "../../DesktopEditor/common/File.h"
#include "../XlsxSerializerCom/Reader/BinaryWriter.h"
#include "../XlsxSerializerCom/Reader/FontProcessor.h"
#include "../XlsxSerializerCom/Writer/BinaryReader.h"
namespace BinXlsxRW{
int g_nCurFormatVersion = 0;
CXlsxSerializer::CXlsxSerializer()
{
m_pInterface = NULL;
m_pExternalDrawingConverter = NULL;
}
CXlsxSerializer::~CXlsxSerializer()
{
RELEASEINTERFACE(m_pExternalDrawingConverter);
}
bool CXlsxSerializer::loadFromFile(CString& sSrcFileName, CString& sDstPath, CString& sXMLOptions, CString& sMediaDir)
{
PPTXFile::IAVSOfficeDrawingConverter* pOfficeDrawingConverter;
CoCreateInstance(__uuidof(PPTXFile::CAVSOfficeDrawingConverter), NULL, CLSCTX_ALL, __uuidof(PPTXFile::IAVSOfficeDrawingConverter), (void**)(&pOfficeDrawingConverter));
BSTR bstrMediaDir = sMediaDir.AllocSysString();
pOfficeDrawingConverter->SetMediaDstPath(bstrMediaDir);
SysFreeString(bstrMediaDir);
//
TCHAR tFolder[256];
TCHAR tDrive[256];
_tsplitpath( sSrcFileName, tDrive, tFolder, NULL, NULL );
CString sFolder = CString(tFolder);
CString sDrive = CString(tDrive);
CString sFileInDir = sDrive + sFolder;
VARIANT var;
var.vt = VT_BSTR;
var.bstrVal = sFileInDir.AllocSysString();
pOfficeDrawingConverter->SetAdditionalParam(L"SourceFileDir2", var);
RELEASESYSSTRING(var.bstrVal);
BinXlsxRW::BinaryFileReader oBinaryFileReader;
oBinaryFileReader.ReadFile(sSrcFileName, sDstPath, pOfficeDrawingConverter, sXMLOptions);
RELEASEINTERFACE(pOfficeDrawingConverter);
return true;
}
bool CXlsxSerializer::saveToFile(CString& sDstFileName, CString& sSrcPath, CString& sXMLOptions)
{
BinXlsxRW::FontProcessor fp;
fp.setFontDir(m_sFontDir);
PPTXFile::IOfficeFontPicker* pFontPicker = NULL;
CoCreateInstance(__uuidof(PPTXFile::COfficeFontPicker), NULL, CLSCTX_ALL, __uuidof(PPTXFile::IOfficeFontPicker), (void**)(&pFontPicker));
BSTR bstrFontDir1 = m_sFontDir.AllocSysString();
pFontPicker->Init(bstrFontDir1);
SysFreeString(bstrFontDir1);
NSFontCutter::CEmbeddedFontsManager* pEmbeddedFontsManager = NULL;
if(false == m_sEmbeddedFontsDir.IsEmpty())
{
NSDirectory::CreateDirectory(string2std_string(m_sEmbeddedFontsDir));
BSTR bstrEmbeddedFontsDirectory = m_sEmbeddedFontsDir.AllocSysString();
pFontPicker->SetEmbeddedFontsDirectory(bstrEmbeddedFontsDirectory);
SysFreeString(bstrEmbeddedFontsDirectory);
VARIANT vt;
pFontPicker->GetAdditionalParam(_T("NativeCutter"), &vt);
pEmbeddedFontsManager = (NSFontCutter::CEmbeddedFontsManager*)vt.pvRecord;
ASCGraphics::IASCFontManager* pFontManager = fp.getFontManager();
//
pEmbeddedFontsManager->CheckFont(_T("Wingdings 3"), pFontManager);
pEmbeddedFontsManager->CheckFont(_T("Arial"), pFontManager);
//pEmbeddedFontsManager
//
pEmbeddedFontsManager->CheckFont(_T("Calibri"), pFontManager);
pEmbeddedFontsManager->CheckString(CString(_T("ABCDEFGHIJKLMNOPQRSTUVWXYZ")));
// "#NULL!", "#DIV/0!"...
pEmbeddedFontsManager->CheckString(CString(_T("#!/?")));
// num id 0 49
pEmbeddedFontsManager->CheckString(CString(_T(".%E+-():")));
}
PPTXFile::IAVSOfficeDrawingConverter* pOfficeDrawingConverter;
CoCreateInstance(__uuidof(PPTXFile::CAVSOfficeDrawingConverter), NULL, CLSCTX_ALL, __uuidof(PPTXFile::IAVSOfficeDrawingConverter), (void**)(&pOfficeDrawingConverter));
BSTR bstrFontDir = m_sFontDir.AllocSysString();
pOfficeDrawingConverter->SetFontDir(bstrFontDir);
SysFreeString(bstrFontDir);
VARIANT vt;
vt.vt = VT_UNKNOWN;
vt.punkVal = pFontPicker;
pOfficeDrawingConverter->SetAdditionalParam(_T("FontPicker"), vt);
BinXlsxRW::BinaryFileWriter oBinaryFileWriter(fp);
oBinaryFileWriter.Open(sSrcPath, sDstFileName, pEmbeddedFontsManager, pOfficeDrawingConverter, sXMLOptions);
RELEASEINTERFACE(pFontPicker);
RELEASEINTERFACE(pOfficeDrawingConverter);
return true;
}
bool CXlsxSerializer::loadChart(CString& sChartPath, unsigned char** ppBinary, long& lDataSize)
{
bool bRes = false;
*ppBinary = NULL;
OOX::Spreadsheet::CChartSpace oChart(sChartPath);
if(NULL != m_pExternalDrawingConverter)
{
Streams::CBuffer oBuffer;
oBuffer.Create(BUFFER_GROW_SIZE, BUFFER_GROW_SIZE);
Streams::CBufferedStream oBufferedStream;
oBufferedStream.SetBuffer(&oBuffer);
BinXlsxRW::BinaryCommonWriter oBcw(oBufferedStream);
BSTR bstrChartPath = sChartPath.AllocSysString();
m_pExternalDrawingConverter->SetRelsPath(bstrChartPath);
SysFreeString(bstrChartPath);
BinXlsxRW::BinaryChartWriter oBinaryChartWriter(oBufferedStream, m_pExternalDrawingConverter);
oBinaryChartWriter.WriteCT_ChartSpace(oChart);
if (NULL != ppBinary)
{
lDataSize = oBufferedStream.GetPosition();
*ppBinary = new unsigned char[lDataSize];
BYTE* pDataS = oBufferedStream.GetBuffer();
memcpy(*ppBinary, pDataS, lDataSize);
bRes = true;
}
}
return bRes;
}
bool CXlsxSerializer::saveChart(SAFEARRAY* pBinaryObj, long lStart, long lLength, CString& sFilepath, CString& sContentTypePath, CString** sContentTypeElement)
{
bool bRes = false;
*sContentTypeElement = NULL;
if(NULL != m_pExternalDrawingConverter)
{
Streams::CBuffer oBuffer;
Streams::CBufferedStream oBufferedStream;
oBufferedStream.SetBuffer(&oBuffer);
oBufferedStream.Create((BYTE*)pBinaryObj->pvData + lStart, lLength);
m_pExternalDrawingConverter->SetDstContentRels();
// sThemePath bsFilename theme bsFilename
CString sThemePath;
CString sFilenameReverse = sFilepath;sFilenameReverse.MakeReverse();
int nIndex = sFilenameReverse.Find('\\');
nIndex = sFilenameReverse.Find('\\', nIndex + 1);
if(-1 != nIndex)
sThemePath = sFilepath.Left(sFilepath.GetLength() - nIndex) + _T("theme");
//todo theme path
BinXlsxRW::SaveParams oSaveParams(sThemePath);
OOX::Spreadsheet::CChartSpace oChartSpace;
BinXlsxRW::BinaryChartReader oBinaryChartReader(oBufferedStream, oSaveParams, pBinaryObj, m_pExternalDrawingConverter);
oBinaryChartReader.ReadCT_ChartSpace(lLength, &oChartSpace.m_oChartSpace);
if(oChartSpace.isValid())
{
TCHAR tDrive[256];
TCHAR tFolder[256];
TCHAR tFilename[256];
TCHAR tExt[256];
_tsplitpath( sFilepath, tDrive, tFolder, tFilename, tExt );
CString sDrive(tDrive);
CString sFolder(tFolder);
CString sFilename(tFilename);
CString sExt(tExt);
CString sRelsDir = sDrive + sFolder;
sRelsDir.Append(_T("_rels"));
DWORD dwFileAttr = ::GetFileAttributes( sRelsDir );
if( dwFileAttr == INVALID_FILE_ATTRIBUTES )
OOX::CSystemUtility::CreateDirectories(sRelsDir);
oChartSpace.write2(sFilepath);
CString sRelsPath;
sRelsPath.Format(_T("%s\\%s.rels"), sRelsDir, sFilename + sExt);
BSTR bstrRelsPath = sRelsPath.AllocSysString();
m_pExternalDrawingConverter->SaveDstContentRels(bstrRelsPath);
SysFreeString(bstrRelsPath);
CString sContentType(sContentTypePath);
sContentType.Append(sFilename);
sContentType.Append(sExt);
(*sContentTypeElement) = new CString();
(*sContentTypeElement)->Format(_T("<Override PartName=\"%s\" ContentType=\"application/vnd.openxmlformats-officedocument.drawingml.chart+xml\"/>"), sContentType);
(*sContentTypeElement)->Append(oSaveParams.sAdditionalContentTypes);
bRes = true;
}
}
return bRes;
}
void CXlsxSerializer::setFontDir(CString& sFontDir)
{
m_sFontDir = sFontDir;
}
void CXlsxSerializer::setEmbeddedFontsDir(CString& sEmbeddedFontsDir)
{
m_sEmbeddedFontsDir = sEmbeddedFontsDir;
}
void CXlsxSerializer::setDrawingConverter(IUnknown* pDocument)
{
RELEASEINTERFACE(m_pExternalDrawingConverter);
pDocument->QueryInterface(__uuidof(PPTXFile::IAVSOfficeDrawingConverter), (void**)&m_pExternalDrawingConverter);
}
#ifdef _WIN32
void CXlsxSerializer::setComInterface(IUnknown* pInterface)
{
m_pInterface = pInterface;
}
#endif
};
\ No newline at end of file
#ifndef XLSX_SERIALIZER
#define XLSX_SERIALIZER
//todo
#include <atlbase.h>
#include <atlstr.h>
#include <string>
namespace PPTXFile{
struct IAVSOfficeDrawingConverter;
}
namespace BinXlsxRW {
class CXlsxSerializer{
private:
CString m_sFontDir;
CString m_sEmbeddedFontsDir;
public:
CXlsxSerializer();
~CXlsxSerializer();
bool loadFromFile(CString& sSrcFileName, CString& sDstPath, CString& sXMLOptions, CString& sMediaDir);
bool saveToFile(CString& sSrcFileName, CString& sDstPath, CString& sXMLOptions);
bool loadChart(CString& sChartPath, unsigned char** ppBinary, long& lDataSize);
bool saveChart(SAFEARRAY* pBinaryObj, long lStart, long lLength, CString& sFilename, CString& sContentTypePath, CString** sContentTypeElement);
void setFontDir(CString& sFontDir);
void setEmbeddedFontsDir(CString& sEmbeddedFontsDir);
void setDrawingConverter(IUnknown* pDocument);
//todo
#ifdef _WIN32
PPTXFile::IAVSOfficeDrawingConverter* m_pExternalDrawingConverter;
IUnknown* m_pInterface;
void setComInterface(IUnknown* pInterface);
#endif
};
}
#endif // #ifndef XLSX_SERIALIZER
\ No newline at end of file
......@@ -121,7 +121,7 @@ public:
}
unsigned char* pData = NULL;
long lDataSize = 0;
bool bRes = m_oCDocxSerializer.GetBinaryContent(BstrToStdString(bsTxContent), &pData, lDataSize);
bool bRes = m_oCDocxSerializer.getBinaryContent(BstrToStdString(bsTxContent), &pData, lDataSize);
if(NULL != pData && lDataSize > 0)
{
SAFEARRAYBOUND rgsabound[1];
......@@ -160,7 +160,7 @@ public:
m_oCDocxSerializer.setComInterface(m_pThis);
}
std::wstring sRes;
bool bRes = m_oCDocxSerializer.GetXmlContent((BYTE*)pBinaryObj->pvData, pBinaryObj->rgsabound[0].cElements, lStart, lLength, sRes);
bool bRes = m_oCDocxSerializer.getXmlContent((BYTE*)pBinaryObj->pvData, pBinaryObj->rgsabound[0].cElements, lStart, lLength, sRes);
(*bsXml) = CString(sRes.c_str()).AllocSysString();
return bRes ? S_OK : S_FALSE;
}
......
......@@ -10,6 +10,7 @@
#define IDB_DEFAULT_DOC_CORE 203
#define IDB_DEFAULT_DOC_THEME 204
#define IDB_DEFAULT_DOC_WEBSETTINGS 205
#define IDB_DEFAULT_XLSX_THEME 206
// Next default values for new objects
//
......
#pragma once
#include "resource.h"
#include "../Common/OfficeFileTemplate.h"
#include "DocWrapper/XlsxSerializer.h"
#include "../XlsxSerializerCom/Common/Common.h"
#include "../XlsxSerializerCom/Common/BinReaderWriterDefines.h"
#include "../DesktopEditor/common/Directory.h"
[
object,
uuid("B7AD8AA6-A103-4260-82BE-338C32543B90"),
dual, helpstring("IAVSOfficeXlsxSerizer Interface"),
pointer_default(unique)
]
__interface IAVSOfficeXlsxSerizer : public IAVSOfficeFileTemplate
{
[id(20)] HRESULT SetFontDir([in] BSTR bsFontDir);
[id(30)] HRESULT LoadChart([in] BSTR bsFilename, [out, satype("BYTE")] SAFEARRAY** ppBinary);
[id(31)] HRESULT SaveChart([in, satype("BYTE")] SAFEARRAY* pBinaryObj, [in] LONG lStart, [in] LONG lLength, [in] BSTR bsFilename, [in] BSTR bsContentTypePath, [out] BSTR* bsContentTypeElement);
[id(32)] HRESULT SetDrawingConverter([in] IUnknown* pDocument);
[id(120)] HRESULT SetAdditionalParam([in] BSTR ParamName, [in] VARIANT ParamValue);
[id(130)] HRESULT GetAdditionalParam([in] BSTR ParamName, [out, retval] VARIANT* ParamValue);
};
[
coclass,
default(IAVSOfficeXlsxSerizer),
event_receiver(native),
threading(apartment),
vi_progid("ASCOfficeXlsxSerizer.AVSDocume"),
progid("ASCOfficeXlsxSerizer.AVSDocu.1"),
version(1.0),
uuid("BDE1A2A6-2EE0-4507-BD2E-0C42DA4101C2"),
helpstring("ASCOfficeXlsxSerizer Class")
]
class ATL_NO_VTABLE CAVSOfficeXlsxSerizer : public IAVSOfficeXlsxSerizer
{
private:
BinXlsxRW::CXlsxSerializer m_oXlsxSerializer;
IUnknown* m_pThis;
public:
CAVSOfficeXlsxSerizer()
{
}
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
public:
STDMETHOD(LoadFromFile)(BSTR sSrcFileName, BSTR sDstPath, BSTR bstrXMLOptions)
{
if(NULL == m_pThis)
{
this->QueryInterface( __uuidof(IUnknown), (void**)&m_pThis );
m_oXlsxSerializer.setComInterface(m_pThis);
}
CString sMediaDir = sDstPath;
sMediaDir.Append(_T("\\xl\\media"));
// File Type ( , , ReadFile , )
BYTE fileType;
UINT nCodePage;
WCHAR wcDelimiter;
SerializeCommon::ReadFileType(CString(bstrXMLOptions), fileType, nCodePage, wcDelimiter);
if (BinXlsxRW::c_oFileTypes::CSV != fileType)
{
CString sXlDir;sXlDir.Format(_T("%s\\xl"), sDstPath);
CString sThemeDir;sThemeDir.Format(_T("%s\\xl\\%s"), sDstPath, OOX::FileTypes::Theme.DefaultDirectory());
CString sThemePath;sThemePath.Format(_T("%s\\%s"), sThemeDir, OOX::FileTypes::Theme.DefaultFileName());
CString sThemeRelsPath;sThemeRelsPath.Format(_T("%s\\_rels"), sThemeDir);
NSDirectory::CreateDirectory(string2std_string(sXlDir));
NSDirectory::CreateDirectory(string2std_string(sThemeDir));
NSDirectory::CreateDirectory(string2std_string(sThemeRelsPath));
NSDirectory::CreateDirectory(string2std_string(sMediaDir));
CreateTheme(sThemePath);
}
bool bRes = m_oXlsxSerializer.loadFromFile(CString(sSrcFileName), CString(sDstPath), CString(bstrXMLOptions), sMediaDir);
return bRes ? S_OK : S_FALSE;
}
STDMETHOD(SaveToFile)(BSTR sDstFileName, BSTR sSrcPath, BSTR sXMLOptions)
{
if(NULL == m_pThis)
{
this->QueryInterface( __uuidof(IUnknown), (void**)&m_pThis );
m_oXlsxSerializer.setComInterface(m_pThis);
}
bool bRes = m_oXlsxSerializer.saveToFile(CString(sDstFileName), CString(sSrcPath), CString(sXMLOptions)) ? S_OK : S_FALSE;
return bRes ? S_OK : S_FALSE;
}
STDMETHOD(LoadChart)(BSTR bsFilename, SAFEARRAY** ppBinary)
{
if(NULL == m_pThis)
{
this->QueryInterface( __uuidof(IUnknown), (void**)&m_pThis );
m_oXlsxSerializer.setComInterface(m_pThis);
}
unsigned char* pData = NULL;
long lDataSize = 0;
bool bRes = m_oXlsxSerializer.loadChart(CString(bsFilename), &pData, lDataSize);
if(NULL != pData && lDataSize > 0)
{
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = lDataSize;
LPSAFEARRAY pArray = SafeArrayCreate(VT_UI1, 1, rgsabound);
BYTE* pDataD = (BYTE*)pArray->pvData;
BYTE* pDataS = pData;
memcpy(pDataD, pDataS, lDataSize);
*ppBinary = pArray;
}
RELEASEARRAYOBJECTS(pData);
return bRes ? S_OK : S_FALSE;
}
STDMETHOD(SaveChart)(SAFEARRAY* pBinaryObj, LONG lStart, LONG lLength, BSTR bsFilename, BSTR bsContentTypePath, BSTR* bsContentTypeElement)
{
if(NULL == m_pThis)
{
this->QueryInterface( __uuidof(IUnknown), (void**)&m_pThis );
m_oXlsxSerializer.setComInterface(m_pThis);
}
CString* sContentTypeElement = NULL;
bool bRes = m_oXlsxSerializer.saveChart(pBinaryObj, lStart, lLength, CString(bsFilename), CString(bsContentTypePath), &sContentTypeElement);
if(NULL != sContentTypeElement)
*bsContentTypeElement = sContentTypeElement->AllocSysString();
return bRes ? S_OK : S_FALSE;
}
STDMETHOD(SetDrawingConverter)(IUnknown* pDocument)
{
m_oXlsxSerializer.setDrawingConverter(pDocument);
return S_OK;
}
STDMETHOD(SetFontDir)(BSTR bsFontDir)
{
m_oXlsxSerializer.setFontDir(CString(bsFontDir));
return S_OK;
}
STDMETHOD(SetAdditionalParam)(BSTR ParamName, VARIANT ParamValue)
{
CString sParamName; sParamName = ParamName;
if (_T("EmbeddedFontsDirectory") == sParamName && ParamValue.vt == VT_BSTR)
{
m_oXlsxSerializer.setEmbeddedFontsDir(CString(ParamValue.bstrVal));
return S_OK;
}
return S_OK;
}
STDMETHOD(GetAdditionalParam)(BSTR ParamName, VARIANT* ParamValue)
{
return S_OK;
}
private:
void CreateTheme(CString sThemePath)
{
HINSTANCE hInst = _AtlBaseModule.GetModuleInstance();
LoadResourceFile(hInst, MAKEINTRESOURCE(IDB_DEFAULT_XLSX_THEME), _T("XLSXSER"), sThemePath);
}
void LoadResourceFile(HINSTANCE hInst, LPCTSTR sResName, LPCTSTR sResType, const CString& strDstFile)
{
HRSRC hrRes = FindResource(hInst, sResName, sResType);
if (!hrRes)
return;
HGLOBAL hGlobal = LoadResource(hInst, hrRes);
DWORD sz = SizeofResource(hInst, hrRes);
void* ptrRes = LockResource(hGlobal);
CFile oFile;
oFile.CreateFile(strDstFile);
oFile.WriteFile(ptrRes, sz);
UnlockResource(hGlobal);
FreeResource(hGlobal);
}
};
\ No newline at end of file
#pragma once
#ifndef XLSX_BIN_READER_WRITER_DEFINES
#define XLSX_BIN_READER_WRITER_DEFINES
namespace BinXlsxRW
{
......@@ -716,3 +717,5 @@ namespace BinXlsxRW
TabColor = 9
};}
}
#endif //
\ No newline at end of file
#pragma once
#ifndef SERIALIZER_COMMON
#define SERIALIZER_COMMON
#ifdef _WIN32
#include <atlbase.h>
......@@ -10,6 +11,8 @@
#include <string>
#include <vector>
#define BUFFER_GROW_SIZE 1 * 1024 * 1024 //1mb
namespace SerializeCommon
{
bool IsUnicodeSymbol( WCHAR symbol );
......@@ -45,4 +48,6 @@ namespace SerializeCommon
}
};
void ReadFileType(CString& sXMLOptions, BYTE& result, UINT& nCodePage, WCHAR& wcDelimiter);
}
\ No newline at end of file
}
#endif //SERIALIZER_COMMON
\ No newline at end of file
#include "CSVReader.h"
#include "../../Common/MappingFile.h"
namespace CSVReader
{
void AddCell(CString &sText, INT nStartCell, std::stack<INT> &oDeleteChars, OOX::Spreadsheet::CRow &oRow, INT nRow, INT nCol, BOOL bIsWrap)
{
while(!oDeleteChars.empty())
{
INT nIndex = oDeleteChars.top() - nStartCell;
sText.Delete(nIndex);
oDeleteChars.pop();
}
//
if (0 == sText.GetLength())
return;
OOX::Spreadsheet::CCell *pCell = new OOX::Spreadsheet::CCell();
pCell->m_oType.Init();
WCHAR *pEndPtr;
LONG lValue = wcstol(sText, &pEndPtr, 10);
if (NULL != *pEndPtr)
{
//
pCell->m_oType->SetValue(SimpleTypes::Spreadsheet::celltypeInlineStr);
pCell->m_oRichText.Init();
OOX::Spreadsheet::CText *pText = new OOX::Spreadsheet::CText();
pText->m_sText = sText;
pCell->m_oRichText->m_arrItems.push_back(pText);
}
else
{
//
pCell->m_oValue.Init();
pCell->m_oValue->m_sText = sText;
}
if (bIsWrap)
{
// WrapStyle
pCell->m_oStyle.Init();
pCell->m_oStyle->SetValue(1);
}
pCell->m_oRef.Init();
pCell->m_oRef = OOX::Spreadsheet::CWorksheet::combineRef(nRow, nCol);
oRow.m_arrItems.push_back(pCell);
}
void ReadFromCsvToXlsx(CString &sFileName, OOX::Spreadsheet::CXlsx &oXlsx, UINT nCodePage, CONST WCHAR wcDelimiter)
{
// Workbook
oXlsx.CreateWorkbook();
//
oXlsx.CreateStyles();
// wrap-
OOX::Spreadsheet::CStyles *pStyles = oXlsx.GetStyles();
pStyles->m_oCellXfs.Init();
pStyles->m_oCellXfs->m_oCount.Init();
pStyles->m_oCellXfs->m_oCount->SetValue(2);
// Normall default
OOX::Spreadsheet::CXfs* pXfs = NULL;
pXfs = new OOX::Spreadsheet::CXfs();
pXfs->m_oBorderId.Init();
pXfs->m_oBorderId->SetValue(0);
pXfs->m_oFillId.Init();
pXfs->m_oFillId->SetValue(0);
pXfs->m_oFontId.Init();
pXfs->m_oFontId->SetValue(0);
pXfs->m_oNumFmtId.Init();
pXfs->m_oNumFmtId->SetValue(0);
pStyles->m_oCellXfs->m_arrItems.push_back(pXfs);
// Wrap style
pXfs = new OOX::Spreadsheet::CXfs();
pXfs->m_oBorderId.Init();
pXfs->m_oBorderId->SetValue(0);
pXfs->m_oFillId.Init();
pXfs->m_oFillId->SetValue(0);
pXfs->m_oFontId.Init();
pXfs->m_oFontId->SetValue(0);
pXfs->m_oNumFmtId.Init();
pXfs->m_oNumFmtId->SetValue(0);
pXfs->m_oApplyAlignment.Init();
pXfs->m_oApplyAlignment->SetValue(SimpleTypes::onoffTrue);
pXfs->m_oAligment.Init();
pXfs->m_oAligment->m_oWrapText.Init();
pXfs->m_oAligment->m_oWrapText->SetValue(SimpleTypes::onoffTrue);
pStyles->m_oCellXfs->m_arrItems.push_back(pXfs);
CString sSheetRId = _T("rId1");
OOX::Spreadsheet::CWorksheet* pWorksheet = new OOX::Spreadsheet::CWorksheet();
pWorksheet->m_oSheetData.Init();
OOX::Spreadsheet::CSheet *pSheet = new OOX::Spreadsheet::CSheet();
pSheet->m_oName.Init();
pSheet->m_oName->Append(_T("Sheet1"));
pSheet->m_oSheetId.Init();
pSheet->m_oSheetId->SetValue(1);
pSheet->m_oRid.Init();
pSheet->m_oRid->SetValue(sSheetRId);
OOX::Spreadsheet::CWorkbook *pWorkbook = oXlsx.GetWorkbook();
pWorkbook->m_oSheets.Init();
pWorkbook->m_oSheets->m_arrItems.push_back(pSheet);
MemoryMapping::CMappingFile oMappingFile = MemoryMapping::CMappingFile();
if(FALSE != oMappingFile.Open(sFileName))
{
long nFileSize = oMappingFile.GetSize();
LPCSTR pFileData = (LPCSTR)oMappingFile.GetData();
INT nSize = MultiByteToWideChar(nCodePage, 0, pFileData, nFileSize, NULL, 0);
WCHAR *pTemp = new WCHAR [nSize];
::ZeroMemory (pTemp, sizeof(WCHAR) * nSize);
MultiByteToWideChar (nCodePage, 0, pFileData, nFileSize, pTemp, nSize);
oMappingFile.Close();
CONST WCHAR wcNewLineN = _T('\n');
CONST WCHAR wcNewLineR = _T('\r');
CONST WCHAR wcQuote = _T('"');
CONST WCHAR wcTab = _T('\t');
BOOL bIsWrap = FALSE;
WCHAR wcCurrent;
INT nStartCell = 0;
std::stack<INT> oDeleteChars;
BOOL bInQuote = FALSE;
INT nIndexRow = 0;
INT nIndexCol = 0;
OOX::Spreadsheet::CRow *pRow = new OOX::Spreadsheet::CRow();
pRow->m_oR.Init();
pRow->m_oR->SetValue(nIndexRow + 1);
for (INT nIndex = 0; nIndex < nSize; ++nIndex)
{
wcCurrent = pTemp[nIndex];
if (wcDelimiter == wcCurrent)
{
if (bInQuote)
continue;
// New Cell
CString sCellText(pTemp + nStartCell, nIndex - nStartCell);
AddCell(sCellText, nStartCell, oDeleteChars, *pRow, nIndexRow, nIndexCol++, bIsWrap);
bIsWrap = FALSE;
nStartCell = nIndex + 1;
if (nStartCell == nSize)
{
pWorksheet->m_oSheetData->m_arrItems.push_back(pRow);
pRow = NULL;
}
}
else if (wcNewLineN == wcCurrent || wcNewLineR == wcCurrent)
{
if (bInQuote)
{
// Wrap
bIsWrap = TRUE;
continue;
}
// New line
if (nStartCell != nIndex)
{
CString sCellText(pTemp + nStartCell, nIndex - nStartCell);
AddCell(sCellText, nStartCell, oDeleteChars, *pRow, nIndexRow, nIndexCol++, bIsWrap);
bIsWrap = FALSE;
}
if (wcNewLineR == wcCurrent && nIndex + 1 != nSize && wcNewLineN == pTemp[nIndex + 1])
{
// \r\n 1
++nIndex;
}
nStartCell = nIndex + 1;
pWorksheet->m_oSheetData->m_arrItems.push_back(pRow);
pRow = new OOX::Spreadsheet::CRow();
pRow->m_oR.Init();
pRow->m_oR->SetValue(++nIndexRow + 1);
nIndexCol = 0;
}
else if (wcQuote == wcCurrent)
{
// Quote
if (FALSE == bInQuote && nStartCell == nIndex && nIndex + 1 != nSize)
{
// ( )
bInQuote = !bInQuote;
nStartCell = nIndex + 1;
}
else if (TRUE == bInQuote)
{
//
oDeleteChars.push(nIndex);
// , (1997,Ford,E350,"Super, ""luxurious"" truck")
if (nIndex + 1 != nSize && wcQuote == pTemp[nIndex + 1])
++nIndex;
else
bInQuote = !bInQuote;
}
}
else if (wcTab == wcCurrent)
{
// delete tab if not delimiter
oDeleteChars.push(nIndex);
}
}
if (nStartCell != nSize)
{
// New line
CString sCellText(pTemp + nStartCell, nSize - nStartCell);
AddCell(sCellText, nStartCell, oDeleteChars, *pRow, nIndexRow, nIndexCol++, bIsWrap);
pWorksheet->m_oSheetData->m_arrItems.push_back(pRow);
}
else
{
RELEASEOBJECT(pRow);
}
RELEASEARRAYOBJECTS(pTemp);
}
CAtlMap<CString, OOX::Spreadsheet::CWorksheet*> &arrWorksheets = oXlsx.GetWorksheets();
arrWorksheets.SetAt(sSheetRId, pWorksheet);
}
}
\ No newline at end of file
#pragma once
#include <stack>
namespace CSVReader
{
void AddCell(CString &sText, INT nStartCell, std::stack<INT> &oDeleteChars, OOX::Spreadsheet::CRow &oRow, INT nRow, INT nCol, BOOL bIsWrap)
{
while(!oDeleteChars.empty())
{
INT nIndex = oDeleteChars.top() - nStartCell;
sText.Delete(nIndex);
oDeleteChars.pop();
}
//
if (0 == sText.GetLength())
return;
OOX::Spreadsheet::CCell *pCell = new OOX::Spreadsheet::CCell();
pCell->m_oType.Init();
WCHAR *pEndPtr;
LONG lValue = wcstol(sText, &pEndPtr, 10);
if (NULL != *pEndPtr)
{
//
pCell->m_oType->SetValue(SimpleTypes::Spreadsheet::celltypeInlineStr);
pCell->m_oRichText.Init();
OOX::Spreadsheet::CText *pText = new OOX::Spreadsheet::CText();
pText->m_sText = sText;
pCell->m_oRichText->m_arrItems.push_back(pText);
}
else
{
//
pCell->m_oValue.Init();
pCell->m_oValue->m_sText = sText;
}
if (bIsWrap)
{
// WrapStyle
pCell->m_oStyle.Init();
pCell->m_oStyle->SetValue(1);
}
pCell->m_oRef.Init();
pCell->m_oRef = OOX::Spreadsheet::CWorksheet::combineRef(nRow, nCol);
oRow.m_arrItems.push_back(pCell);
}
void ReadFromCsvToXlsx(CString &sFileName, OOX::Spreadsheet::CXlsx &oXlsx, UINT nCodePage, CONST WCHAR wcDelimiter)
{
// Workbook
oXlsx.CreateWorkbook();
//
oXlsx.CreateStyles();
// wrap-
OOX::Spreadsheet::CStyles *pStyles = oXlsx.GetStyles();
pStyles->m_oCellXfs.Init();
pStyles->m_oCellXfs->m_oCount.Init();
pStyles->m_oCellXfs->m_oCount->SetValue(2);
// Normall default
OOX::Spreadsheet::CXfs* pXfs = NULL;
pXfs = new OOX::Spreadsheet::CXfs();
pXfs->m_oBorderId.Init();
pXfs->m_oBorderId->SetValue(0);
pXfs->m_oFillId.Init();
pXfs->m_oFillId->SetValue(0);
pXfs->m_oFontId.Init();
pXfs->m_oFontId->SetValue(0);
pXfs->m_oNumFmtId.Init();
pXfs->m_oNumFmtId->SetValue(0);
pStyles->m_oCellXfs->m_arrItems.push_back(pXfs);
// Wrap style
pXfs = new OOX::Spreadsheet::CXfs();
pXfs->m_oBorderId.Init();
pXfs->m_oBorderId->SetValue(0);
pXfs->m_oFillId.Init();
pXfs->m_oFillId->SetValue(0);
pXfs->m_oFontId.Init();
pXfs->m_oFontId->SetValue(0);
pXfs->m_oNumFmtId.Init();
pXfs->m_oNumFmtId->SetValue(0);
pXfs->m_oApplyAlignment.Init();
pXfs->m_oApplyAlignment->SetValue(SimpleTypes::onoffTrue);
pXfs->m_oAligment.Init();
pXfs->m_oAligment->m_oWrapText.Init();
pXfs->m_oAligment->m_oWrapText->SetValue(SimpleTypes::onoffTrue);
pStyles->m_oCellXfs->m_arrItems.push_back(pXfs);
#ifndef CSV_READER
#define CSV_READER
CString sSheetRId = _T("rId1");
OOX::Spreadsheet::CWorksheet* pWorksheet = new OOX::Spreadsheet::CWorksheet();
pWorksheet->m_oSheetData.Init();
OOX::Spreadsheet::CSheet *pSheet = new OOX::Spreadsheet::CSheet();
pSheet->m_oName.Init();
pSheet->m_oName->Append(_T("Sheet1"));
pSheet->m_oSheetId.Init();
pSheet->m_oSheetId->SetValue(1);
pSheet->m_oRid.Init();
pSheet->m_oRid->SetValue(sSheetRId);
OOX::Spreadsheet::CWorkbook *pWorkbook = oXlsx.GetWorkbook();
pWorkbook->m_oSheets.Init();
pWorkbook->m_oSheets->m_arrItems.push_back(pSheet);
MemoryMapping::CMappingFile oMappingFile = MemoryMapping::CMappingFile();
if(FALSE != oMappingFile.Open(sFileName))
{
long nFileSize = oMappingFile.GetSize();
LPCSTR pFileData = (LPCSTR)oMappingFile.GetData();
INT nSize = MultiByteToWideChar(nCodePage, 0, pFileData, nFileSize, NULL, 0);
WCHAR *pTemp = new WCHAR [nSize];
::ZeroMemory (pTemp, sizeof(WCHAR) * nSize);
MultiByteToWideChar (nCodePage, 0, pFileData, nFileSize, pTemp, nSize);
oMappingFile.Close();
CONST WCHAR wcNewLineN = _T('\n');
CONST WCHAR wcNewLineR = _T('\r');
CONST WCHAR wcQuote = _T('"');
CONST WCHAR wcTab = _T('\t');
BOOL bIsWrap = FALSE;
WCHAR wcCurrent;
INT nStartCell = 0;
std::stack<INT> oDeleteChars;
BOOL bInQuote = FALSE;
INT nIndexRow = 0;
INT nIndexCol = 0;
OOX::Spreadsheet::CRow *pRow = new OOX::Spreadsheet::CRow();
pRow->m_oR.Init();
pRow->m_oR->SetValue(nIndexRow + 1);
for (INT nIndex = 0; nIndex < nSize; ++nIndex)
{
wcCurrent = pTemp[nIndex];
if (wcDelimiter == wcCurrent)
{
if (bInQuote)
continue;
// New Cell
CString sCellText(pTemp + nStartCell, nIndex - nStartCell);
AddCell(sCellText, nStartCell, oDeleteChars, *pRow, nIndexRow, nIndexCol++, bIsWrap);
bIsWrap = FALSE;
nStartCell = nIndex + 1;
if (nStartCell == nSize)
{
pWorksheet->m_oSheetData->m_arrItems.push_back(pRow);
pRow = NULL;
}
}
else if (wcNewLineN == wcCurrent || wcNewLineR == wcCurrent)
{
if (bInQuote)
{
// Wrap
bIsWrap = TRUE;
continue;
}
// New line
if (nStartCell != nIndex)
{
CString sCellText(pTemp + nStartCell, nIndex - nStartCell);
AddCell(sCellText, nStartCell, oDeleteChars, *pRow, nIndexRow, nIndexCol++, bIsWrap);
bIsWrap = FALSE;
}
if (wcNewLineR == wcCurrent && nIndex + 1 != nSize && wcNewLineN == pTemp[nIndex + 1])
{
// \r\n 1
++nIndex;
}
nStartCell = nIndex + 1;
pWorksheet->m_oSheetData->m_arrItems.push_back(pRow);
pRow = new OOX::Spreadsheet::CRow();
pRow->m_oR.Init();
pRow->m_oR->SetValue(++nIndexRow + 1);
nIndexCol = 0;
}
else if (wcQuote == wcCurrent)
{
// Quote
if (FALSE == bInQuote && nStartCell == nIndex && nIndex + 1 != nSize)
{
// ( )
bInQuote = !bInQuote;
nStartCell = nIndex + 1;
}
else if (TRUE == bInQuote)
{
//
oDeleteChars.push(nIndex);
#include <stack>
// , (1997,Ford,E350,"Super, ""luxurious"" truck")
if (nIndex + 1 != nSize && wcQuote == pTemp[nIndex + 1])
++nIndex;
else
bInQuote = !bInQuote;
}
}
else if (wcTab == wcCurrent)
{
// delete tab if not delimiter
oDeleteChars.push(nIndex);
}
}
#ifdef _WIN32
#include <atlbase.h>
#include <atlstr.h>
#else
#include "../../Common/DocxFormat/Source/Base/ASCString.h"
#endif
if (nStartCell != nSize)
{
// New line
CString sCellText(pTemp + nStartCell, nSize - nStartCell);
AddCell(sCellText, nStartCell, oDeleteChars, *pRow, nIndexRow, nIndexCol++, bIsWrap);
pWorksheet->m_oSheetData->m_arrItems.push_back(pRow);
}
else
{
RELEASEOBJECT(pRow);
}
#include "../../Common/DocxFormat/Source/XlsxFormat/Xlsx.h"
RELEASEARRAYOBJECTS(pTemp);
}
namespace CSVReader
{
void AddCell(CString &sText, INT nStartCell, std::stack<INT> &oDeleteChars, OOX::Spreadsheet::CRow &oRow, INT nRow, INT nCol, BOOL bIsWrap);
void ReadFromCsvToXlsx(CString &sFileName, OOX::Spreadsheet::CXlsx &oXlsx, UINT nCodePage, CONST WCHAR wcDelimiter);
}
CAtlMap<CString, OOX::Spreadsheet::CWorksheet*> &arrWorksheets = oXlsx.GetWorksheets();
arrWorksheets.SetAt(sSheetRId, pWorksheet);
}
}
\ No newline at end of file
#endif //CSV_READER
\ No newline at end of file
#include "CSVWriter.h"
namespace CSVWriter
{
void WriteFile(NSFile::CFileBinary *pFile, WCHAR **pWriteBuffer, INT &nCurrentIndex, CString &sWriteString, UINT &nCodePage, BOOL bIsEnd)
{
if (NULL == pFile || NULL == pWriteBuffer)
return;
INT nCountChars = sWriteString.GetLength();
if (0 == nCountChars && !bIsEnd)
return;
CONST INT c_nSize = 1048576; // 1024 * 1024
CONST INT nSizeWchar = sizeof(WCHAR);
if (NULL == *pWriteBuffer)
{
*pWriteBuffer = new WCHAR[c_nSize];
::ZeroMemory(*pWriteBuffer, nSizeWchar * c_nSize);
nCurrentIndex = 0;
}
if (nCountChars + nCurrentIndex > c_nSize || bIsEnd)
{
// ,
if (nCodePage == CP_UTF16)
{
pFile->WriteFile((BYTE*)*pWriteBuffer, sizeof (WCHAR) * nCurrentIndex);
}
else
{
INT nSize = WideCharToMultiByte(nCodePage, 0, *pWriteBuffer, nCurrentIndex, NULL, NULL, NULL, NULL);
CHAR *pString = new CHAR [nSize];
::ZeroMemory (pString, sizeof (CHAR) * nSize);
WideCharToMultiByte (CP_UTF8, 0, *pWriteBuffer, -1, pString, nSize, NULL, NULL);
pFile->WriteFile((BYTE*)pString, sizeof (CHAR) * nSize);
RELEASEARRAYOBJECTS(pString);
}
::ZeroMemory(*pWriteBuffer, nSizeWchar * c_nSize);
nCurrentIndex = 0;
}
if (!bIsEnd)
{
::CopyMemory(*pWriteBuffer + nCurrentIndex, sWriteString.GetBuffer(), nCountChars * nSizeWchar);
nCurrentIndex += nCountChars;
}
}
void WriteFromXlsxToCsv(CString &sFileDst, OOX::Spreadsheet::CXlsx &oXlsx, UINT nCodePage, CONST WCHAR wcDelimiter)
{
NSFile::CFileBinary oFile;
oFile.CreateFileW(string2std_string(sFileDst));
//
if (CP_UTF8 == nCodePage)
{
UCHAR arUTF8[3] = {0xEF, 0xBB, 0xBF};
oFile.WriteFile(arUTF8, 3);
}
else if (CP_UTF16 == nCodePage)
{
UCHAR arUTF16[2] = {0xFF, 0xFE};
oFile.WriteFile(arUTF16, 2);
}
else if (CP_unicodeFFFE == nCodePage)
{
UCHAR arBigEndian[2] = {0xFE, 0xFF};
oFile.WriteFile(arBigEndian, 2);
}
LONG lActiveSheet = 0;
CString sSheetRId = _T("Sheet1"); // rId,
OOX::Spreadsheet::CWorkbook *pWorkbook = oXlsx.GetWorkbook();
if (NULL != pWorkbook)
{
// Get active sheet
if (pWorkbook->m_oBookViews.IsInit() && 0 < pWorkbook->m_oBookViews->m_arrItems.size())
{
if (pWorkbook->m_oBookViews->m_arrItems[0]->m_oActiveTab.IsInit())
{
lActiveSheet = pWorkbook->m_oBookViews->m_arrItems[0]->m_oActiveTab->GetValue();
if (0 > lActiveSheet)
lActiveSheet = 0;
}
}
// Get active sheet rId
if (pWorkbook->m_oSheets.IsInit() && 0 <= pWorkbook->m_oSheets->m_arrItems.size())
{
if (lActiveSheet <= pWorkbook->m_oSheets->m_arrItems.size())
sSheetRId = pWorkbook->m_oSheets->m_arrItems[lActiveSheet]->m_oName.get2();
else
sSheetRId = pWorkbook->m_oSheets->m_arrItems[0]->m_oName.get2();
}
CAtlMap<CString, OOX::Spreadsheet::CWorksheet*> &arrWorksheets = oXlsx.GetWorksheets();
CAtlMap<CString, OOX::Spreadsheet::CWorksheet*>::CPair* pPair = arrWorksheets.Lookup(sSheetRId);
if (NULL != pPair)
{
OOX::Spreadsheet::CWorksheet *pWorksheet = pPair->m_value;
if (NULL != pWorksheet && pWorksheet->m_oSheetData.IsInit())
{
OOX::Spreadsheet::CSharedStrings *pSharedStrings = oXlsx.GetSharedStrings();
CString sNewLineN = _T("\n");
CString sDelimiter = _T(""); sDelimiter += wcDelimiter;
CONST WCHAR wcQuote = _T('"');
CString sEscape = _T("\"\n");
sEscape += wcDelimiter;
INT nCurrentIndex = 0;
WCHAR *pWriteBuffer = NULL;
INT nRowCurrent = 1;
for (INT i = 0; i < pWorksheet->m_oSheetData->m_arrItems.size(); ++i)
{
OOX::Spreadsheet::CRow *pRow = static_cast<OOX::Spreadsheet::CRow *>(pWorksheet->m_oSheetData->m_arrItems[i]);
INT nRow = pRow->m_oR.IsInit() ? pRow->m_oR->GetValue() : 0 == i ? nRowCurrent : nRowCurrent + 1;
while (nRow > nRowCurrent)
{
// Write new line
++nRowCurrent;
WriteFile(&oFile, &pWriteBuffer, nCurrentIndex, sNewLineN, nCodePage);
}
INT nColCurrent = 1;
for (INT j = 0; j < pRow->m_arrItems.size(); ++j)
{
INT nRowTmp = 0;
INT nCol = 0;
if (!OOX::Spreadsheet::CWorksheet::parseRef(pRow->m_arrItems[j]->m_oRef.get2(), nRowTmp, nCol))
nCol = 0 == j ? nColCurrent : nColCurrent + 1;
while (nCol > nColCurrent)
{
// Write delimiter
++nColCurrent;
WriteFile(&oFile, &pWriteBuffer, nCurrentIndex, sDelimiter, nCodePage);
}
OOX::Spreadsheet::CCell *pCell = static_cast<OOX::Spreadsheet::CCell *>(pRow->m_arrItems[j]);
// Get cell value
CString sCellValue = _T("");
if (pCell->m_oValue.IsInit())
{
if (pCell->m_oType.IsInit() && SimpleTypes::Spreadsheet::celltypeNumber != pCell->m_oType->GetValue())
{
int nValue = _wtoi(pCell->m_oValue->ToString());
if (0 <= nValue && nValue < pSharedStrings->m_arrItems.size())
{
OOX::Spreadsheet::CSi *pSi = static_cast<OOX::Spreadsheet::CSi *>(pSharedStrings->m_arrItems[nValue]);
if (NULL != pSi && pSi->m_arrItems.size() > 0)
if(NULL != pSi && pSi->m_arrItems.size() > 0)
{
OOX::Spreadsheet::WritingElement* pWe = pSi->m_arrItems[0];
if(OOX::Spreadsheet::et_t == pWe->getType())
{
OOX::Spreadsheet::CText* pText = static_cast<OOX::Spreadsheet::CText*>(pWe);
sCellValue = pText->m_sText;
}
}
}
}
else
{
sCellValue = pCell->m_oValue->ToString();
}
}
// Escape cell value
if (-1 != sCellValue.FindOneOf(sEscape))
{
sCellValue.Replace(_T("\""), _T("\"\""));
sCellValue = wcQuote + sCellValue + wcQuote;
}
// Write cell value
WriteFile(&oFile, &pWriteBuffer, nCurrentIndex, sCellValue, nCodePage);
}
}
WriteFile(&oFile, &pWriteBuffer, nCurrentIndex, sNewLineN, nCodePage, TRUE);
RELEASEARRAYOBJECTS(pWriteBuffer);
}
}
}
oFile.CloseFile();
}
}
\ No newline at end of file
#pragma once
#ifndef CSV_WRITER
#define CSV_WRITER
#define CP_UTF16 1200
#define CP_unicodeFFFE 1201
namespace CSVWriter
{
void WriteFile(CFile *pFile, WCHAR **pWriteBuffer, INT &nCurrentIndex, CString &sWriteString, UINT &nCodePage, BOOL bIsEnd = FALSE)
{
if (NULL == pFile || NULL == pWriteBuffer)
return;
INT nCountChars = sWriteString.GetLength();
if (0 == nCountChars && !bIsEnd)
return;
CONST INT c_nSize = 1048576; // 1024 * 1024
CONST INT nSizeWchar = sizeof(WCHAR);
if (NULL == *pWriteBuffer)
{
*pWriteBuffer = new WCHAR[c_nSize];
::ZeroMemory(*pWriteBuffer, nSizeWchar * c_nSize);
nCurrentIndex = 0;
}
if (nCountChars + nCurrentIndex > c_nSize || bIsEnd)
{
// ,
if (nCodePage == CP_UTF16)
{
pFile->WriteFile(*pWriteBuffer, sizeof (WCHAR) * nCurrentIndex);
}
else
{
INT nSize = WideCharToMultiByte(nCodePage, 0, *pWriteBuffer, nCurrentIndex, NULL, NULL, NULL, NULL);
CHAR *pString = new CHAR [nSize];
::ZeroMemory (pString, sizeof (CHAR) * nSize);
WideCharToMultiByte (CP_UTF8, 0, *pWriteBuffer, -1, pString, nSize, NULL, NULL);
pFile->WriteFile(pString, sizeof (CHAR) * nSize);
RELEASEARRAYOBJECTS(pString);
}
::ZeroMemory(*pWriteBuffer, nSizeWchar * c_nSize);
nCurrentIndex = 0;
}
if (!bIsEnd)
{
::CopyMemory(*pWriteBuffer + nCurrentIndex, sWriteString.GetBuffer(), nCountChars * nSizeWchar);
nCurrentIndex += nCountChars;
}
}
void WriteFromXlsxToCsv(CString &sFileDst, OOX::Spreadsheet::CXlsx &oXlsx, UINT nCodePage, CONST WCHAR wcDelimiter)
{
CFile oFile;
oFile.CreateFileW(sFileDst);
//
if (CP_UTF8 == nCodePage)
{
UCHAR arUTF8[3] = {0xEF, 0xBB, 0xBF};
oFile.WriteFile(arUTF8, 3);
}
else if (CP_UTF16 == nCodePage)
{
UCHAR arUTF16[2] = {0xFF, 0xFE};
oFile.WriteFile(arUTF16, 2);
}
else if (CP_unicodeFFFE == nCodePage)
{
UCHAR arBigEndian[2] = {0xFE, 0xFF};
oFile.WriteFile(arBigEndian, 2);
}
LONG lActiveSheet = 0;
CString sSheetRId = _T("Sheet1"); // rId,
OOX::Spreadsheet::CWorkbook *pWorkbook = oXlsx.GetWorkbook();
if (NULL != pWorkbook)
{
// Get active sheet
if (pWorkbook->m_oBookViews.IsInit() && 0 < pWorkbook->m_oBookViews->m_arrItems.size())
{
if (pWorkbook->m_oBookViews->m_arrItems[0]->m_oActiveTab.IsInit())
{
lActiveSheet = pWorkbook->m_oBookViews->m_arrItems[0]->m_oActiveTab->GetValue();
if (0 > lActiveSheet)
lActiveSheet = 0;
}
}
// Get active sheet rId
if (pWorkbook->m_oSheets.IsInit() && 0 <= pWorkbook->m_oSheets->m_arrItems.size())
{
if (lActiveSheet <= pWorkbook->m_oSheets->m_arrItems.size())
sSheetRId = pWorkbook->m_oSheets->m_arrItems[lActiveSheet]->m_oName.get2();
else
sSheetRId = pWorkbook->m_oSheets->m_arrItems[0]->m_oName.get2();
}
CAtlMap<CString, OOX::Spreadsheet::CWorksheet*> &arrWorksheets = oXlsx.GetWorksheets();
CAtlMap<CString, OOX::Spreadsheet::CWorksheet*>::CPair* pPair = arrWorksheets.Lookup(sSheetRId);
if (NULL != pPair)
{
OOX::Spreadsheet::CWorksheet *pWorksheet = pPair->m_value;
if (NULL != pWorksheet && pWorksheet->m_oSheetData.IsInit())
{
OOX::Spreadsheet::CSharedStrings *pSharedStrings = oXlsx.GetSharedStrings();
CString sNewLineN = _T("\n");
CString sDelimiter = _T(""); sDelimiter += wcDelimiter;
CONST WCHAR wcQuote = _T('"');
CString sEscape = _T("\"\n");
sEscape += wcDelimiter;
#ifdef _WIN32
#include <atlbase.h>
#include <atlstr.h>
#else
#include "../../Common/DocxFormat/Source/Base/ASCString.h"
#endif
INT nCurrentIndex = 0;
WCHAR *pWriteBuffer = NULL;
#include "../../DesktopEditor/common/File.h"
#include "../../Common/DocxFormat/Source/XlsxFormat/Xlsx.h"
INT nRowCurrent = 1;
for (INT i = 0; i < pWorksheet->m_oSheetData->m_arrItems.size(); ++i)
{
OOX::Spreadsheet::CRow *pRow = static_cast<OOX::Spreadsheet::CRow *>(pWorksheet->m_oSheetData->m_arrItems[i]);
INT nRow = pRow->m_oR.IsInit() ? pRow->m_oR->GetValue() : 0 == i ? nRowCurrent : nRowCurrent + 1;
while (nRow > nRowCurrent)
{
// Write new line
++nRowCurrent;
WriteFile(&oFile, &pWriteBuffer, nCurrentIndex, sNewLineN, nCodePage);
}
INT nColCurrent = 1;
for (INT j = 0; j < pRow->m_arrItems.size(); ++j)
{
INT nRowTmp = 0;
INT nCol = 0;
if (!OOX::Spreadsheet::CWorksheet::parseRef(pRow->m_arrItems[j]->m_oRef.get2(), nRowTmp, nCol))
nCol = 0 == j ? nColCurrent : nColCurrent + 1;
while (nCol > nColCurrent)
{
// Write delimiter
++nColCurrent;
WriteFile(&oFile, &pWriteBuffer, nCurrentIndex, sDelimiter, nCodePage);
}
OOX::Spreadsheet::CCell *pCell = static_cast<OOX::Spreadsheet::CCell *>(pRow->m_arrItems[j]);
// Get cell value
CString sCellValue = _T("");
if (pCell->m_oValue.IsInit())
{
if (pCell->m_oType.IsInit() && SimpleTypes::Spreadsheet::celltypeNumber != pCell->m_oType->GetValue())
{
int nValue = _wtoi(pCell->m_oValue->ToString());
if (0 <= nValue && nValue < pSharedStrings->m_arrItems.size())
{
OOX::Spreadsheet::CSi *pSi = static_cast<OOX::Spreadsheet::CSi *>(pSharedStrings->m_arrItems[nValue]);
if (NULL != pSi && pSi->m_arrItems.size() > 0)
if(NULL != pSi && pSi->m_arrItems.size() > 0)
{
OOX::Spreadsheet::WritingElement* pWe = pSi->m_arrItems[0];
if(OOX::Spreadsheet::et_t == pWe->getType())
{
OOX::Spreadsheet::CText* pText = static_cast<OOX::Spreadsheet::CText*>(pWe);
sCellValue = pText->m_sText;
}
}
}
}
else
{
sCellValue = pCell->m_oValue->ToString();
}
}
// Escape cell value
if (-1 != sCellValue.FindOneOf(sEscape))
{
sCellValue.Replace(_T("\""), _T("\"\""));
sCellValue = wcQuote + sCellValue + wcQuote;
}
// Write cell value
WriteFile(&oFile, &pWriteBuffer, nCurrentIndex, sCellValue, nCodePage);
}
}
WriteFile(&oFile, &pWriteBuffer, nCurrentIndex, sNewLineN, nCodePage, TRUE);
RELEASEARRAYOBJECTS(pWriteBuffer);
}
}
}
namespace CSVWriter
{
void WriteFile(NSFile::CFileBinary *pFile, WCHAR **pWriteBuffer, INT &nCurrentIndex, CString &sWriteString, UINT &nCodePage, BOOL bIsEnd = FALSE);
void WriteFromXlsxToCsv(CString &sFileDst, OOX::Spreadsheet::CXlsx &oXlsx, UINT nCodePage, CONST WCHAR wcDelimiter);
}
oFile.CloseFile();
}
}
\ No newline at end of file
#endif //CSV_WRITER
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment