2012-06-21 38 views
1

我想寫一些電話號碼excel文件,其中一些從0開始(如02167820096)。 我嘗試在列的NumberFormatLocal屬性設置爲字符串類型:如何通過Qt設置excel列格式?

 QAxObject* col=worksheet->querySubObject("Columns(int)",1); 
     if (!col) 
     { 
      qDebug()<<"col is NULL"; 
     } 
     qDebug()<<"col 1 NumberFormatLocal:"<<col->property("NumberFormatLocal").toString(); 
     col->setProperty("NumberFormatLocal","@"); 
     qDebug()<<"col 1 NumberFormatLocal:"<<col->property("NumberFormatLocal").toString(); 

輸出是

col 1 NumberFormatLocal: "G/通用格式" 
col 1 NumberFormatLocal: "@" 

,我可以看到在第一列的單元格確實被設置爲字符串類型(「@ 「)。

  QAxObject * range = worksheet->querySubObject("Cells(int,int)", 1, 1); 
      if (!range) 
      { 
       qDebug()<<"range does not exist"; 
      } 
      QVariant tel=QString("%1").arg(record["tel"].toString()); //tel is 02167820096 
      //qDebug()<<tel; 
      //range->dynamicCall("SetValue(const QVariant&)", tel); 
      qDebug()<<"NumberFormatLocal:"<<range->property("NumberFormatLocal").toString(); 
      qDebug()<<"NumberFormat:"<<range->property("NumberFormat").toString(); 
      range->setProperty("Value", tel.toString()); 
      range->clear(); 

和輸出

NumberFormatLocal: "@" 
NumberFormat: "@" 

,但是當我打開保存Excel文件時,它被標記爲普通型的所有單元格,代碼沒有在所有的工作!

幫助!謝謝...

+0

對不起,現在沒關係。 – areslp

+0

我也試過這樣:範圍 - > dynamicCall( 「SetNumberFormatLocal(常量的QVariant&)」 的QVariant( 「@」));但它不工作,要麼...... – areslp

+0

'範圍 - >清除();'你爲什麼清晰的範圍? – Lol4t0

回答

0

OK,我解決了這個問題。因爲我安裝Office2007中的「保存」功能,將文件保存在2007年的格式,但我將它保存爲「* .xls的」,一些格式不能被正確識別。

因此,解決辦法是,使用「另存爲」,以保存Excel 2003格式。

 QList<QVariant> lstParam; 
     qDebug()<<QDir::toNativeSeparators(file_path); 
     lstParam.append(QDir::toNativeSeparators(file_path)); 
     lstParam.append(-4143); 
     lstParam.append(""); 
     lstParam.append(""); 
     lstParam.append(false); 
     lstParam.append(false); 
     lstParam.append(1); 
     lstParam.append(2); 
     lstParam.append(false); 
     lstParam.append(false); 
     lstParam.append(false); 
     lstParam.append(false); 
     QVariant res = workbook->dynamicCall("SaveAs(QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant)", lstParam); 
     if(res.toBool()) 
     { 
      qDebug()<<"SaveAs successful"; 
     } 
相關問題