W ColdFusion 9.0.1 (tj. Updater 1), jeśli użyjesz SpreadsheetSetCellValue(), będzie to zgodne z wcześniej ustawionym formatem. Aby wymusić na kolumnie tekst podczas wypełniania arkusza, można użyć procesu 3-etapowego:
- Wypełnij arkusz kalkulacyjny, ignorując niepoprawnie zinterpretowane wartości liczbowe.
- Sformatuj kolumnę, która ma być tekstem.
- Zastąp nieprawidłową wartość w każdym wierszu kolumny poprawną wartością, która będzie teraz traktowana jako tekst.
Oto przykład, który można skopiować do .cfm i biegać jak jest (wymaga CF9.0.1)
<cfscript>
// Create a 2 column, 2 row query. The first column contains numbers or possible numbers we want formatted as text in our spreadsheet
q = QueryNew("");
QueryAddColumn(q,"NumbersAsText","VarChar",[ 01050094071094340000,"743059E6" ]);
QueryAddColumn(q,"Text","VarChar",[ "abc","def" ]);
// Get the column names as an array so we can get at them more easily later
columns = q.getMetaData().getColumnLabels();
// Create a new spreadsheet object
sheet = SpreadSheetNew("test");
// specify the column we want formatted as text
forceTextColumnNumber = 1;
// Use the query column names as column headers in our sheet
SpreadSheetAddRow(sheet,q.columnList);
// Add the data: the numbers will be inserted as numeric for now
SpreadSheetAddRows(sheet,q);
// Now we format the column as text
SpreadSheetFormatColumn(sheet,{ dataformat="text" },forceTextColumnNumber);
// Having formatted the column, add the column from our query again so the values correct
while(q.next())
{
// Skip the header row by adding one
rownumber = (q.currentrow + 1);
// Get the value of column at the current row in the loop
value = q[ columns[ forceTextColumnNumber ] ][ q.currentrow ];
// replace the previously added numeric value which will now be treated as text
SpreadsheetSetCellValue(sheet,value,rownumber,forceTextColumnNumber);
}
// Download the object as a file
sheetAsBinary = SpreadSheetReadBinary(sheet);
filename = "test.xls";
</cfscript>
<cfheader name="Content-Disposition" value="attachment; filename=#Chr(34)##filename##Chr(34)#">
<cfcontent type="application/msexcel" variable="#sheetAsBinary#" reset="true">
Domyślnie obie wartości w pierwszej kolumnie mojego zapytania byłoby być traktowane jak liczby (drugie jako HEX). Użycie tej metody powoduje zachowanie pierwotnej wartości jako tekstu.
Dzięki, okazuje się, że "tekst" to również możliwość (ten sam wynik), ale trzeba je nazwać PO wypełnieniu rzędów (co moim zdaniem nie ma sensu), ponieważ zamiast "01050094071094340000" (żądana moc wyjściowa) Mam '1.050094071094339e + 18' wydaje się, że jest to DUŻE wydanie (przynajmniej dla mnie) Używając zwykłego "automatycznego" formatu, usunie wszystkie wiodące zera – raulriera