Compare commits

..

3 Commits

Author SHA1 Message Date
acfd12ea30 fix bug #36740 2018-02-01 16:42:34 +03:00
a04b83cada . 2018-02-01 12:28:46 +03:00
a7bf12c052 . 2018-01-31 18:17:45 +03:00
18 changed files with 330 additions and 247 deletions

View File

@ -416,7 +416,7 @@ namespace MathEquation
virtual void BeginEquation()
{
int nCurPos = WriteItemStart(BinDocxRW::c_oSerParType::OMath);
int nCurPos = WriteItemStart(BinDocxRW::c_oSer_OMathContentType::OMath);
m_aEquationStack.push(nCurPos);
}
virtual void EndEquation()
@ -618,7 +618,7 @@ namespace MathEquation
nRows = m_aRowsCounter.top();
m_aRowsCounter.pop();
}
int nPos = m_oStream.GetPosition();
int nPos = 0;
if (!m_aRowsPosCounter.empty())
{
nPos = m_aRowsPosCounter.top();

View File

@ -137,10 +137,10 @@ namespace BinXlsxRW{
oOfficeDrawingConverter.SetFontPicker(pFontPicker);
BinXlsxRW::BinaryFileWriter oBinaryFileWriter(fp);
oBinaryFileWriter.Open(sSrcPath, sDstFileName, pEmbeddedFontsManager, &oOfficeDrawingConverter, sXMLOptions, m_bIsNoBase64);
bool result = oBinaryFileWriter.Open(sSrcPath, sDstFileName, pEmbeddedFontsManager, &oOfficeDrawingConverter, sXMLOptions, m_bIsNoBase64);
RELEASEOBJECT(pFontPicker);
return true;
return result;
}
bool CXlsxSerializer::saveChart(NSBinPptxRW::CBinaryFileReader* pReader, long lLength, const std::wstring& sFilepath, const long& lChartNumber)
{

View File

@ -777,7 +777,8 @@ void docx_conversion_context::process_section(std::wostream & strm, odf_reader::
{
double page_width = 0;
const odf_reader::page_layout_instance * pp = root()->odf_context().pageLayoutContainer().page_layout_first();
if (pp)
if ((pp) && (pp->properties()))
{
odf_reader::style_page_layout_properties_attlist & attr_page = pp->properties()->attlist_;
if (attr_page.fo_page_width_)

View File

@ -233,8 +233,9 @@ void object_odf_context::docx_convert(oox::docx_conversion_context & Context)
Context.set_paragraph_state (false);
Context.set_run_state (false);
Context.get_math_context().base_font_size_ = baseFontHeight_;
Context.start_math_formula();
Context.get_math_context().base_font_size_ = baseFontHeight_;
office_math_->oox_convert(Context.get_math_context());
Context.end_math_formula();

View File

@ -61,6 +61,7 @@ private:
document_context::document_context() : impl_( new document_context::Impl() )
{
level = 0;
}
document_context::~document_context()

View File

@ -45,10 +45,11 @@ class document_context
public:
document_context();
virtual ~document_context();
public:
void set_last_paragraph(text::paragraph * Paragraph);
text::paragraph * get_last_paragraph();
int level;
private:
class Impl;

View File

@ -151,6 +151,8 @@ void office_body::docx_convert(oox::docx_conversion_context & Context)
{
if (page_layout_instance * lastPageLayout = Context.root()->odf_context().pageLayoutContainer().page_layout_by_name(Context.get_page_properties()))
{
Context.next_dump_page_properties(true);
lastPageLayout->docx_serialize(Context.output_stream(), Context);
//Context.remove_page_properties();
}

View File

@ -213,7 +213,7 @@ void style_table_column_properties::docx_convert(oox::docx_conversion_context &
double kf_max_width_ms =1.;
const page_layout_instance * pp = Context.root()->odf_context().pageLayoutContainer().page_layout_first();//
if (pp)
if ((pp) && (pp->properties()))
{
style_page_layout_properties_attlist & attr_page = pp->properties()->attlist_;
if (attr_page.fo_page_width_)

View File

@ -312,14 +312,29 @@ void paragraph::afterCreate(document_context * Context)
// вызывается сразу после создания объекта
if (Context)
{
// выставляем у предыдущего параграфа указатель на следующий (т.е. на вновь созданный)
if (paragraph * prevPar = Context->get_last_paragraph())
prevPar->set_next(this);
// запоминаем в контексте вновь созданный параграф
Context->set_last_paragraph(this);
Context->level++;
// выставляем у предыдущего параграфа указатель на следующий (т.е. на вновь созданный)
if (Context->level == 1)
{
if (paragraph * prevPar = Context->get_last_paragraph())
{
prevPar->set_next(this);
}
// запоминаем в контексте вновь созданный параграф
Context->set_last_paragraph(this);
}
}
}
void paragraph::afterReadContent(document_context * Context)
{
if (Context)
{
Context->level--;
}
}
const wchar_t * emptyParagraphContent = L"<w:pPr></w:pPr><w:r><w:rPr></w:rPr></w:r>";
const wchar_t * emptyParagraphDrawing = L"<w:p><w:pPr></w:pPr></w:p>";
@ -435,27 +450,6 @@ void paragraph::docx_convert(oox::docx_conversion_context & Context)
std::wostream & _Wostream = Context.output_stream();
if (next_par_)
{
// проверяем не сменит ли следующий параграф свойства страницы.
// если да — устанавливаем контексту флаг на то что необходимо в конце текущего параграфа
// распечатать свойства раздела
//проверить ... не она ли текущая - может быть прописан дубляж - и тогда разрыв нарисуется ненужный
const std::wstring & next_styleName = next_par_->attrs_.text_style_name_;
const _CP_OPT(std::wstring) next_masterPageName = Context.root()->odf_context().styleContainer().master_page_name_by_name(next_styleName);
if ((next_masterPageName) && (Context.get_master_page_name() != *next_masterPageName))
{
Context.next_dump_page_properties(true);
is_empty = false;
}
}
if (next_section_ || next_end_section_)
{
Context.get_section_context().get().is_dump_ = true;
is_empty = false;
}
const _CP_OPT(std::wstring) masterPageName = Context.root()->odf_context().styleContainer().master_page_name_by_name(styleName);
if (masterPageName)
@ -469,6 +463,27 @@ void paragraph::docx_convert(oox::docx_conversion_context & Context)
is_empty = false;
}
if (next_par_)
{
// проверяем не сменит ли следующий параграф свойства страницы.
// если да — устанавливаем контексту флаг на то что необходимо в текущем параграфе
// распечатать свойства раздела/секции
//проверить ... не она ли текущая - может быть прописан дубляж - и тогда разрыв нарисуется ненужный
const std::wstring & next_styleName = next_par_->attrs_.text_style_name_;
const _CP_OPT(std::wstring) next_masterPageName = Context.root()->odf_context().styleContainer().master_page_name_by_name(next_styleName);
if ((next_masterPageName) && (Context.get_master_page_name() != *next_masterPageName))
{
Context.next_dump_page_properties(true);
is_empty = false;
}
}
if (next_section_/* || next_end_section_*/)
{
Context.get_section_context().get().is_dump_ = true;
is_empty = false;
}
std::wstringstream strm;
if (Context.process_page_properties(strm))
{
@ -604,7 +619,10 @@ void h::afterCreate()
{
paragraph_.afterCreate( getContext() );
}
void h::afterReadContent()
{
paragraph_.afterReadContent( getContext() );
}
void h::docx_convert(oox::docx_conversion_context & Context)
{
paragraph_.docx_convert(Context);
@ -627,7 +645,10 @@ void p::afterCreate()
{
paragraph_.afterCreate( getContext() );
}
void p::afterReadContent()
{
paragraph_.afterReadContent( getContext() );
}
std::wostream & p::text_to_stream(std::wostream & _Wostream) const
{
return paragraph_.text_to_stream(_Wostream);

View File

@ -73,7 +73,8 @@ public:
}
void afterCreate(document_context * ctx);
void afterReadContent(document_context * ctx);
void docx_convert (oox::docx_conversion_context & Context) ;
void xlsx_convert (oox::xlsx_conversion_context & Context) ;
void pptx_convert (oox::pptx_conversion_context & Context) ;
@ -114,7 +115,9 @@ public:
void pptx_convert(oox::pptx_conversion_context & Context) ;
virtual void afterCreate();
virtual std::wostream & text_to_stream(std::wostream & _Wostream) const;
virtual void afterReadContent();
virtual std::wostream & text_to_stream(std::wostream & _Wostream) const;
paragraph paragraph_;
@ -151,8 +154,9 @@ public:
void pptx_convert(oox::pptx_conversion_context & Context) ;
virtual void afterCreate();
virtual std::wostream & text_to_stream(std::wostream & _Wostream) const;
virtual void afterReadContent();
virtual std::wostream & text_to_stream(std::wostream & _Wostream) const;
p(){};
paragraph paragraph_;
@ -245,7 +249,8 @@ public:
void docx_convert(oox::docx_conversion_context & Context);
virtual std::wostream & text_to_stream(std::wostream & _Wostream) const;
virtual void afterCreate();
virtual void afterCreate();
virtual void afterReadContent();
private:

View File

@ -262,6 +262,8 @@ void DocxConverter::convert_document()
//----------------------------------------------------------------------------------------------------------
convert(docx_document->m_pDocument->m_oSectPr.GetPointer(), false, L"Standard");
odt_context->text_context()->clear_params();
for (size_t sect = 0; sect < sections.size(); sect++)
@ -1412,7 +1414,7 @@ void DocxConverter::apply_HF_from(OOX::Logic::CSectionProperty *props, OOX::Logi
}
}
}
void DocxConverter::convert(OOX::Logic::CSectionProperty *oox_section_pr, bool bSection)
void DocxConverter::convert(OOX::Logic::CSectionProperty *oox_section_pr, bool bSection, const std::wstring & master_name)
{
if (oox_section_pr == NULL) return;
current_section_properties = NULL;
@ -1437,18 +1439,24 @@ void DocxConverter::convert(OOX::Logic::CSectionProperty *oox_section_pr, bool b
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (!last_section_properties && (!bSection || continuous == false || oox_section_pr->m_oTitlePg.IsInit()))
{
last_section_properties = oox_section_pr;
}
else if (!bSection || continuous == false)
bool bDefault = (master_name == L"Standard");
if (!bDefault)
{
apply_HF_from(last_section_properties, oox_section_pr);
if (!last_section_properties && (!bSection || continuous == false || oox_section_pr->m_oTitlePg.IsInit()))
{
last_section_properties = oox_section_pr;
}
else if (!bSection || continuous == false)
{
apply_HF_from(last_section_properties, oox_section_pr);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (!bSection || continuous == false)
{
odt_context->page_layout_context()->add_master_page(bSection ? L"" : L"Standard");
odt_context->page_layout_context()->add_master_page(master_name);
}
bool present_header = false;
@ -1585,7 +1593,7 @@ void DocxConverter::convert(OOX::Logic::CSectionProperty *oox_section_pr, bool b
if (continuous == false || oox_section_pr->m_oTitlePg.IsInit())
{
OOX::Logic::CSectionProperty* s = last_section_properties;
OOX::Logic::CSectionProperty* s = last_section_properties ? last_section_properties : oox_section_pr;
bool present_title_page = s->m_oTitlePg.IsInit() ? true : false;
bool present_odd_even_pages = odt_context->page_layout_context()->even_and_left_headers_;
@ -1652,7 +1660,8 @@ void DocxConverter::convert(OOX::Logic::CSectionProperty *oox_section_pr, bool b
if (!add_odd_even_pages_footer && present_odd_even_pages) odt_context->add_empty_footer(1);
if (!add_default_footer && (present_odd_even_pages || present_title_page)) odt_context->add_empty_footer(0);
odt_context->is_paragraph_in_current_section_ = true;
if (!bDefault)
odt_context->is_paragraph_in_current_section_ = true;
//odt_context->set_master_page_name(odt_context->page_layout_context()->last_master() ?
// odt_context->page_layout_context()->last_master()->get_name() : L"");
@ -1694,24 +1703,27 @@ void DocxConverter::convert(OOX::Logic::CSectionProperty *oox_section_pr, bool b
odt_context->add_section_columns(num_columns,
oox_section_pr->m_oCols->m_arrColumns.size() > 0 ? -1 : default_space_pt , separator );
std::vector<std::pair<double,double>> width_space;
for (size_t i = 0; i< oox_section_pr->m_oCols->m_arrColumns.size(); i++)
if (num_columns > 1) //
{
if (oox_section_pr->m_oCols->m_arrColumns[i] == NULL) continue;
std::vector<std::pair<double,double>> width_space;
double space = default_space_pt;
if (oox_section_pr->m_oCols->m_arrColumns[i]->m_oSpace.IsInit())
space = oox_section_pr->m_oCols->m_arrColumns[i]->m_oSpace->ToPoints();
double w = -1;
if (oox_section_pr->m_oCols->m_arrColumns[i]->m_oW.IsInit())
w = oox_section_pr->m_oCols->m_arrColumns[i]->m_oW->ToPoints();
for (size_t i = 0; i < oox_section_pr->m_oCols->m_arrColumns.size(); i++)
{
if (oox_section_pr->m_oCols->m_arrColumns[i] == NULL) continue;
double space = default_space_pt;
if (oox_section_pr->m_oCols->m_arrColumns[i]->m_oSpace.IsInit())
space = oox_section_pr->m_oCols->m_arrColumns[i]->m_oSpace->ToPoints();
width_space.push_back(std::pair<double,double>(w, space));
}
double w = -1;
if (oox_section_pr->m_oCols->m_arrColumns[i]->m_oW.IsInit())
w = oox_section_pr->m_oCols->m_arrColumns[i]->m_oW->ToPoints();
width_space.push_back(std::pair<double,double>(w, space));
}
odt_context->add_section_column(width_space);
odt_context->add_section_column(width_space);
}
}
}
void DocxConverter::convert(OOX::Logic::CBackground *oox_background, int type)

View File

@ -184,7 +184,7 @@ namespace Oox2Odf
void convert(OOX::Logic::CBackground *oox_background, int type);
void convert(OOX::Logic::CSdt *oox_sdt);
void convert(OOX::Logic::CSectionProperty *oox_section_pr, bool bSection);
void convert(OOX::Logic::CSectionProperty *oox_section_pr, bool bSection, const std::wstring & master_name = L"");
void convert(OOX::Logic::CParagraph *oox_paragraph);
void convert(OOX::Logic::CRun *oox_run);
void convert(OOX::Logic::CParagraphProperty *oox_para_prop, odf_writer::style_paragraph_properties *paragraph_properties);

View File

@ -335,6 +335,17 @@ namespace PPTX
std::wstring strOlePath = pReader->GetString(_embed_data_size);
m_OleObjectFile->set_filename(strOlePath, false); //temp !!! for ImageManager original file name
}
else if (embedded_type == 4)
{
pReader->Seek(pReader->GetPos() - 4); //roll back to size record
std::wstring sXmlContent;
pReader->m_pMainDocument->getXmlContentElem(OOX::et_m_oMathPara, *pReader, sXmlContent);
if (!sXmlContent.empty())
{
m_sAlternateContenteXml = sXmlContent;
}
}
else if (embedded_type == 1)
{
m_OleObjectFile = new OOX::OleObject(NULL, true, pReader->m_nDocumentType == XMLWRITER_DOC_TYPE_DOCX);

View File

@ -221,6 +221,8 @@ namespace PPTX
nullable_int m_oDxaOrig;
nullable_int m_oDyaOrig;
nullable_string m_sAlternateContenteXml;
};
class Pic : public WrapperWritingElement

View File

@ -637,7 +637,7 @@ namespace NExtractTools
{
std::wstring sRes;
int nCsvEncoding = 46;//65001 utf8
std::wstring cDelimiter = L"";
std::wstring cDelimiter = L",";
if(NULL != m_nCsvTxtEncoding)
nCsvEncoding = *m_nCsvTxtEncoding;

View File

@ -4322,9 +4322,11 @@ namespace BinXlsxRW
{
RELEASEOBJECT(m_oBcw);
}
void Open(const std::wstring& sInputDir, const std::wstring& sFileDst, NSFontCutter::CEmbeddedFontsManager* pEmbeddedFontsManager,
bool Open(const std::wstring& sInputDir, const std::wstring& sFileDst, NSFontCutter::CEmbeddedFontsManager* pEmbeddedFontsManager,
NSBinPptxRW::CDrawingConverter* pOfficeDrawingConverter, const std::wstring& sXMLOptions, bool bIsNoBase64)
{
bool result = true;
OOX::CPath path(sFileDst);
//создаем папку для media
std::wstring mediaDir = path.GetDirectory() + L"media";
@ -4348,17 +4350,26 @@ namespace BinXlsxRW
OOX::Spreadsheet::CXlsx *pXlsx = NULL;
switch(fileType)
{
case BinXlsxRW::c_oFileTypes::CSV:
pXlsx = new OOX::Spreadsheet::CXlsx();
CSVReader::ReadFromCsvToXlsx(sInputDir, *pXlsx, nCodePage, sDelimiter);
break;
case BinXlsxRW::c_oFileTypes::XLSX:
default:
pXlsx = new OOX::Spreadsheet::CXlsx(OOX::CPath(sInputDir));
break;
case BinXlsxRW::c_oFileTypes::CSV:
{
pXlsx = new OOX::Spreadsheet::CXlsx();
result = CSVReader::ReadFromCsvToXlsx(sInputDir, *pXlsx, nCodePage, sDelimiter);
}break;
case BinXlsxRW::c_oFileTypes::XLSX:
default:
{
pXlsx = new OOX::Spreadsheet::CXlsx(OOX::CPath(sInputDir));
}break;
}
pXlsx->PrepareWorkbook();
if (false == result || NULL == pXlsx->m_pWorkbook)
{
RELEASEOBJECT(pXlsx);
return false;
}
if (BinXlsxRW::c_oFileTypes::JSON == saveFileType)
{
//todo 46 временно CP_UTF8
@ -4393,11 +4404,17 @@ namespace BinXlsxRW
oFile.WriteFile(pbBase64Buffer, nBase64BufferLen);
oFile.CloseFile();
}
else
{
result = false;
}
RELEASEARRAYOBJECTS(pbBase64Buffer);
}
}
RELEASEOBJECT(pXlsx);
return result;
}
void intoBindoc(OOX::Spreadsheet::CXlsx &oXlsx, NSBinPptxRW::CBinaryFileWriter &oBufferedStream, NSFontCutter::CEmbeddedFontsManager* pEmbeddedFontsManager, NSBinPptxRW::CDrawingConverter* pOfficeDrawingConverter)
{

View File

@ -225,19 +225,22 @@ namespace CSVReader
pCell->setRowCol(nRow, nCol);
oRow.m_arrItems.push_back(pCell);
}
void ReadFromCsvToXlsx(const std::wstring &sFileName, OOX::Spreadsheet::CXlsx &oXlsx, UINT nCodePage, const std::wstring& sDelimiter)
bool ReadFromCsvToXlsx(const std::wstring &sFileName, OOX::Spreadsheet::CXlsx &oXlsx, UINT nCodePage, const std::wstring& sDelimiter)
{
// Создадим Workbook
NSFile::CFileBinary oFile;
if (false == oFile.OpenFile(sFileName)) return false;
//-----------------------------------------------------------------------------------
// Создадим Workbook
oXlsx.CreateWorkbook();
// Создадим стили
// Создадим стили
oXlsx.CreateStyles();
// Добавим стили для wrap-а
// Добавим стили для wrap-а
oXlsx.m_pStyles->m_oCellXfs.Init();
oXlsx.m_pStyles->m_oCellXfs->m_oCount.Init();
oXlsx.m_pStyles->m_oCellXfs->m_oCount->SetValue(2);
// Normall default
// Normall default
OOX::Spreadsheet::CXfs* pXfs = NULL;
pXfs = new OOX::Spreadsheet::CXfs();
pXfs->m_oBorderId.Init();
@ -251,7 +254,7 @@ namespace CSVReader
oXlsx.m_pStyles->m_oCellXfs->m_arrItems.push_back(pXfs);
// Wrap style
// Wrap style
pXfs = new OOX::Spreadsheet::CXfs();
pXfs->m_oBorderId.Init();
pXfs->m_oBorderId->SetValue(0);
@ -286,176 +289,182 @@ namespace CSVReader
oXlsx.m_pWorkbook->m_oSheets.Init();
oXlsx.m_pWorkbook->m_oSheets->m_arrItems.push_back(pSheet);
NSFile::CFileBinary oFile;
if(oFile.OpenFile(sFileName))
//-----------------------------------------------------------------------------------
DWORD nFileSize = 0;
BYTE* pFileData = new BYTE[oFile.GetFileSize()];
oFile.ReadFile(pFileData, oFile.GetFileSize(), nFileSize);
oFile.CloseFile();
//skip bom
DWORD nInputBufferSize = nFileSize;
BYTE* pInputBuffer = pFileData;
if (nInputBufferSize >= 3 && 0xef == pInputBuffer[0] && 0xbb == pInputBuffer[1] && 0xbf == pInputBuffer[2])
{
DWORD nFileSize = 0;
BYTE* pFileData = new BYTE[oFile.GetFileSize()];
oFile.ReadFile(pFileData, oFile.GetFileSize(), nFileSize);
oFile.CloseFile();
//skip bom
DWORD nInputBufferSize = nFileSize;
BYTE* pInputBuffer = pFileData;
if (nInputBufferSize >= 3 && 0xef == pInputBuffer[0] && 0xbb == pInputBuffer[1] && 0xbf == pInputBuffer[2])
{
nInputBufferSize -= 3;
pInputBuffer += 3;
}
else if (nInputBufferSize >= 2 && ((0xfe == pInputBuffer[0] && 0xff == pInputBuffer[1]) || (0xff == pInputBuffer[0] && 0xfe == pInputBuffer[1])))
{
nInputBufferSize -= 2;
pInputBuffer += 2;
}
std::wstring sFileDataW;
nInputBufferSize -= 3;
pInputBuffer += 3;
}
else if (nInputBufferSize >= 2 && ((0xfe == pInputBuffer[0] && 0xff == pInputBuffer[1]) || (0xff == pInputBuffer[0] && 0xfe == pInputBuffer[1])))
{
nInputBufferSize -= 2;
pInputBuffer += 2;
}
std::wstring sFileDataW;
if (nCodePage == 1000)
{
sFileDataW = ansi_2_unicode(pInputBuffer, nInputBufferSize);
}
else if (nCodePage == 46)//utf-8
{
sFileDataW = utf8_2_unicode(pInputBuffer, nInputBufferSize);
}
else if (nCodePage == 48)//utf-16
{
sFileDataW = utf16_2_unicode(pInputBuffer, nInputBufferSize);
}
else if (nCodePage == 50) // utf-32
{
sFileDataW = utf32_2_unicode(pInputBuffer, nInputBufferSize);
}
else
{
const NSUnicodeConverter::EncodindId& oEncodindId = NSUnicodeConverter::Encodings[nCodePage];
NSUnicodeConverter::CUnicodeConverter oUnicodeConverter;
sFileDataW = oUnicodeConverter.toUnicode((const char*)pInputBuffer, nInputBufferSize, oEncodindId.Name);
}
if (nCodePage == 1000)
{
sFileDataW = ansi_2_unicode(pInputBuffer, nInputBufferSize);
}
else if (nCodePage == 46)//utf-8
{
sFileDataW = utf8_2_unicode(pInputBuffer, nInputBufferSize);
}
else if (nCodePage == 48)//utf-16
{
sFileDataW = utf16_2_unicode(pInputBuffer, nInputBufferSize);
}
else if (nCodePage == 50) // utf-32
{
sFileDataW = utf32_2_unicode(pInputBuffer, nInputBufferSize);
}
else
{
const NSUnicodeConverter::EncodindId& oEncodindId = NSUnicodeConverter::Encodings[nCodePage];
INT nSize = sFileDataW.length();
const WCHAR *pTemp =sFileDataW.c_str();
NSUnicodeConverter::CUnicodeConverter oUnicodeConverter;
sFileDataW = oUnicodeConverter.toUnicode((const char*)pInputBuffer, nInputBufferSize, oEncodindId.Name);
}
//------------------------------------------------------------------------------------------------------------------------------
WCHAR wcDelimiterLeading = L'\0';
WCHAR wcDelimiterTrailing = L'\0';
int nDelimiterSize = 0;
if (sDelimiter.length() > 0)
size_t nSize = sFileDataW.length();
if (nSize < 1 && nInputBufferSize > 0)
{
return false;
}
const WCHAR *pTemp = sFileDataW.c_str();
WCHAR wcDelimiterLeading = L'\0';
WCHAR wcDelimiterTrailing = L'\0';
int nDelimiterSize = 0;
if (sDelimiter.length() > 0)
{
wcDelimiterLeading = sDelimiter[0];
nDelimiterSize = 1;
if (2 == sizeof(wchar_t) && 0xD800 <= wcDelimiterLeading && wcDelimiterLeading <= 0xDBFF && sDelimiter.length() > 1)
{
wcDelimiterLeading = sDelimiter[0];
nDelimiterSize = 1;
if (2 == sizeof(wchar_t) && 0xD800 <= wcDelimiterLeading && wcDelimiterLeading <= 0xDBFF && sDelimiter.length() > 1)
{
wcDelimiterTrailing = sDelimiter[1];
nDelimiterSize = 2;
}
}
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 (wcDelimiterLeading == wcCurrent && (L'\0' == wcDelimiterTrailing || (nIndex + 1 < nSize && wcDelimiterTrailing == pTemp[nIndex + 1])))
{
if (bInQuote)
continue;
// New Cell
std::wstring sCellText(pTemp + nStartCell, nIndex - nStartCell);
AddCell(sCellText, nStartCell, oDeleteChars, *pRow, nIndexRow, nIndexCol++, bIsWrap);
bIsWrap = false;
nStartCell = nIndex + nDelimiterSize;
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)
{
std::wstring 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 ( 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
std::wstring sCellText(pTemp + nStartCell, nSize - nStartCell);
AddCell(sCellText, nStartCell, oDeleteChars, *pRow, nIndexRow, nIndexCol++, bIsWrap);
pWorksheet->m_oSheetData->m_arrItems.push_back(pRow);
}
else
{
RELEASEOBJECT(pRow);
wcDelimiterTrailing = sDelimiter[1];
nDelimiterSize = 2;
}
}
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 (size_t nIndex = 0; nIndex < nSize; ++nIndex)
{
wcCurrent = pTemp[nIndex];
if (wcDelimiterLeading == wcCurrent && (L'\0' == wcDelimiterTrailing || (nIndex + 1 < nSize && wcDelimiterTrailing == pTemp[nIndex + 1])))
{
if (bInQuote)
continue;
// New Cell
std::wstring sCellText(pTemp + nStartCell, nIndex - nStartCell);
AddCell(sCellText, nStartCell, oDeleteChars, *pRow, nIndexRow, nIndexCol++, bIsWrap);
bIsWrap = false;
nStartCell = nIndex + nDelimiterSize;
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)
{
std::wstring 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 ( 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
std::wstring sCellText(pTemp + nStartCell, nSize - nStartCell);
AddCell(sCellText, nStartCell, oDeleteChars, *pRow, nIndexRow, nIndexCol++, bIsWrap);
pWorksheet->m_oSheetData->m_arrItems.push_back(pRow);
}
else
{
RELEASEOBJECT(pRow);
}
oXlsx.m_arWorksheets.push_back(pWorksheet);
oXlsx.m_mapWorksheets.insert(std::make_pair(sSheetRId, pWorksheet));
}
}

View File

@ -41,7 +41,7 @@
namespace CSVReader
{
void AddCell(std::wstring &sText, INT nStartCell, std::stack<INT> &oDeleteChars, OOX::Spreadsheet::CRow &oRow, INT nRow, INT nCol, bool bIsWrap);
void ReadFromCsvToXlsx(const std::wstring &sFileName, OOX::Spreadsheet::CXlsx &oXlsx, UINT nCodePage, const std::wstring& wcDelimiter);
bool ReadFromCsvToXlsx(const std::wstring &sFileName, OOX::Spreadsheet::CXlsx &oXlsx, UINT nCodePage, const std::wstring& wcDelimiter);
}
#endif //CSV_READER