Voglio convertire un file excel in un'immagine (ogni formato è ok) a livello di programmazione (C#). Attualmente sto usando Microsoft Interop Libraries & Office 2007, ma non supporta il salvataggio su un'immagine per impostazione predefinita.Programmare (C#) convertire Excel in un'immagine
Quindi il mio lavoro intorno attuale è la seguente:
- Aprire file di Excel utilizzando Microsoft Interop;
- Individuare l'intervallo massimo (che contiene dati);
- Utilizzare CopyPicture() su tale intervallo, che copierà i dati negli Appunti.
Ora la parte difficile (e miei problemi):
Problema 1:
Uso della classe .NET Appunti, io non sono in grado di ottenere i dati esatti copiati dagli appunti : i dati sono gli stessi, ma in qualche modo la formattazione è distorta (il font dell'intero documento sembra diventare grassetto e un po 'più illeggibile mentre non lo erano); Se si incolla dagli appunti usando mspaint.exe, l'immagine incollata è corretta (e proprio come voglio che sia).
Ho smontato mspaint.exe e ho trovato una funzione che sta utilizzando (OleGetClipboard) per ottenere i dati dagli appunti, ma non riesco a farlo funzionare in C#/.NET.
Altre cose che ho provato erano gli Appunti WINAPI (OpenClipboard, GetClipboardData, CF_ENHMETAFILE), ma i risultati erano gli stessi dell'utilizzo delle versioni .NET.
Problema 2:
Utilizzando la gamma e CopyPicture, se ci sono delle immagini nel foglio Excel, quelle immagini non sono copiati insieme ai dati che circondano negli Appunti.
Parte del codice sorgente di
Excel.Application app = new Excel.Application();
app.Visible = app.ScreenUpdating = app.DisplayAlerts = false;
app.CopyObjectsWithCells = true;
app.CutCopyMode = Excel.XlCutCopyMode.xlCopy;
app.DisplayClipboardWindow = false;
try {
Excel.Workbooks workbooks = null;
Excel.Workbook book = null;
Excel.Sheets sheets = null;
try {
workbooks = app.Workbooks;
book = workbooks.Open(inputFile, false, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
sheets = book.Worksheets;
} catch {
Cleanup(workbooks, book, sheets); //Cleanup function calls Marshal.ReleaseComObject for all passed objects
throw;
}
for (int i = 0; i < sheets.Count; i++) {
Excel.Worksheet sheet = (Excel.Worksheet)sheets.get_Item(i + 1);
Excel.Range myrange = sheet.UsedRange;
Excel.Range rowRange = myrange.Rows;
Excel.Range colRange = myrange.Columns;
int rows = rowRange.Count;
int cols = colRange.Count;
//Following is used to find range with data
string startRange = "A1";
string endRange = ExcelColumnFromNumber(cols) + rows.ToString();
//Skip "empty" excel sheets
if (startRange == endRange) {
Excel.Range firstRange = sheet.get_Range(startRange, endRange);
Excel.Range cellRange = firstRange.Cells;
object text = cellRange.Text;
string strText = text.ToString();
string trimmed = strText.Trim();
if (trimmed == "") {
Cleanup(trimmed, strText, text, cellRange, firstRange, myrange, rowRange, colRange, sheet);
continue;
}
Cleanup(trimmed, strText, text, cellRange, firstRange);
}
Excel.Range range = sheet.get_Range(startRange, endRange);
try {
range.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlPicture);
//Problem here <-------------
//Every attempt to get data from Clipboard fails
} finally {
Cleanup(range);
Cleanup(myrange, rowRange, colRange, sheet);
}
} //end for loop
book.Close(false, Type.Missing, Type.Missing);
workbooks.Close();
Cleanup(book, sheets, workbooks);
} finally {
app.Quit();
Cleanup(app);
GC.Collect();
}
Ottenere i dati dagli appunti utilizzando WINAPI riesce, ma con cattiva qualità. Fonte:
protected virtual void ClipboardToPNG(string filename) {
if (OpenClipboard(IntPtr.Zero)) {
if (IsClipboardFormatAvailable((int)CLIPFORMAT.CF_ENHMETAFILE)) {
int hEmfClp = GetClipboardDataA((int)CLIPFORMAT.CF_ENHMETAFILE);
if (hEmfClp != 0) {
int hEmfCopy = CopyEnhMetaFileA(hEmfClp, null);
if (hEmfCopy != 0) {
Metafile metafile = new Metafile(new IntPtr(hEmfCopy), true);
metafile.Save(filename, ImageFormat.Png);
}
}
}
CloseClipboard();
}
}
Chiunque ha una soluzione? (Sto usando .NET 2.0 btw)
potrebbe condividere il codice sorgente? In che formato vuoi ottenere i dati copiati? Come bitmap? –