Es hängt davon ab, wie Sie die CSV/XLSX Dokument rendern.
In CSV, wenn Sie das Dokument in NotePad öffnen, werden Sie offensichtlich kein Bild sehen. NotePad rendert keine Bilder, nur Text.
import {encode, decode} from 'node-base64-image'; // ES6
// Get the image as base64 string
var base64string = encode("path/to/img");
// Create an arrays object, to convert to CSV
// Note this is an array of arrays. E.g.,
// For an array:
// array = [ "a", "b", "c" ],
// you get the third element by doing:
// array[2] (= "c")
//
// So, for an array of arrays, i.e., 3 arrays containing 3 elements each:
// arrayOfArrays = [ [ "a", "b", "c" ],[ "d", "e", "f" ], [ "g", "h", "i" ] ]
// you get the third element by doing (as above):
// array[2] (= [ "g", "h", "i" ])
// and the third element of the selected array by doing:
// array[2][2] (= "i")
var csvArrays = [
[ "Image Name", "Image base64 Data" ], // Example titles row
[ "name-for-image", base64string ], // Example data row
];
// Convert arrays to CSV
var csvData = $.csv.fromArrays(csvArrays);
// Write the file!
fs.writeFile('csvFile.csv', csvData, (err) => {
if (err) throw err;
console.log('It\'s saved!');
});
EDIT: Wie andere gesagt haben, es in base64
String in eine CSV, als solche Sie node-base64-image und jQuery-CSV mit schreiben könnte dann aus der CSV-Datei zu entschlüsseln, können wir unsere CSV-Datei erhalten wir gespeichert früher:
import {encode, decode} from 'node-base64-image'; // ES6
var csvFile = fs.readFile('csvFile.csv');
var csvDataAsArrays = $.csv.toArrays(csvFile);
// Get the second array from the group of arrays, then the second element from that array, hence `[1][1]`
var csvImgAsBase64 = csvDataAsArrays[1][1];
var imgAsData = decode(csvImgAsBase64);
fs.writeFile("img.jpeg", imgData, function() {
if (err) throw err;
console.log('It\'s saved!');
}
in XLSX, können Sie das Bild einfügen könnte, wie Sie möchten, öffnen Sie die Arbeitsmappe in Excel um das Bild anzuzeigen. Hier ist, wie dies zu tun mit dem Excel for Node Paket:
ws.addImage({
path: './screenshot2.jpeg',
type: 'picture',
position: {
type: 'absoluteAnchor',
x: '1in',
y: '2in'
}
});
Sie können auch an Zellen relative Position:
ws.addImage({
path: './screenshot1.png',
type: 'picture',
position: {
type: 'twoCellAnchor',
from: {
col: 1,
colOff: 0,
row: 10,
rowOff: 0
},
to: {
col: 4,
colOff: 0,
row: 13,
rowOff: 0
}
}
});
Was erwarten Sie geschrieben werden? – Matt
Das eigentliche Bild in der CSV. –
Es sieht so aus, als ob Sie die 'JSON.stringify'-Version der von Ihnen gelesenen utf-8-Daten haben. Wenn Sie etwas anderes brauchen, lesen Sie es nicht als utf-8 oder verwenden Sie kein JSON-basiertes Dienstprogramm. Ich glaube nicht, dass Sie nicht codierte Binärdaten sicher in eine CSV-Datei schreiben können. – Matt