入力Excelファイルを処理して新しいファイルとして保存できるアプリケーションを作成した後、pdfに変換する必要があります。最後に、彼らは電子メールに添付されて送信されます。私が実行している問題は、pdfが適切に変換されていないということです。私は2ページずつ、ページの縦方向の半分だけでなく、ページの2倍の長さを含むようになります。Excel Interop convert Excelファイルがpdfファイルに正しく変換されていません
これはプレビューです:
私が欲しい何より、このようなものです:
私は、この画像を取得するためにオンラインコンバータを使用。
最後に、これはレイアウトファイルをインポートするために使用しているコードです。ファイルのデータを編集して保存し、pdfとしてエクスポートします。スクリプトの終わりに
Application oExcel = new Application();
Workbook oBook = oExcel.Workbooks.Open(string.Format("{0}\\ATH-ICT\\Waspbane Bestellingen\\Resources\\Layout.xlsx", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)));
Worksheet oSheet = (Worksheet)oBook.Worksheets[1];
oSheet.Range["D3"].Value = customer.Name;
oSheet.Range["D4"].Value = customer.Address;
oSheet.Range["D5"].Value = string.Format("{0} {1}", customer.Postal, customer.City);
oSheet.Range["J3"].Value = DateTime.Now.Year + orderid.ToString("D4");
oSheet.Range["J5"].Value = DateTime.Now.ToString("dd/MM/yyyy");
int productNumber = 11;
foreach (Order order in customer.Orders)
{
oSheet.Range["B" + productNumber].Value = order.Amount;
oSheet.Range["D" + productNumber].Value = "st";
oSheet.Range["E" + productNumber].Value = order.Product.Name;
oSheet.Range["I" + productNumber].Formula = string.Format("={0}/{1}", order.Product.Price, 1.21);
oSheet.Range["J" + productNumber].Formula = "=PRODUCT(B11,I11)";
oSheet.Range["K" + productNumber].Value = 1;
productNumber += 2;
}
oSheet.Range["J36"].Value = shipping;
oSheet.Range["A43"].Value = string.Format("Betaald met {0}", method);
oBook.SaveAs(string.Format("{0}\\Factuur {1}{2:D4}.xlsx", savelocation, DateTime.Now.Year, orderid));
oBook.Close();
oBook = null;
oExcel.Quit();
oExcel = null;
oExcel = new Application();
oBook = oExcel.Workbooks.Open(string.Format("{0}\\Factuur {1}{2:D4}.xlsx", savelocation, DateTime.Now.Year, orderid));
oSheet = (Worksheet)oExcel.Worksheets[1];
oSheet.PageSetup.FitToPagesWide = 1;
oSheet.PageSetup.FitToPagesTall = 1;
oSheet.PageSetup.Zoom = false;
oBook.ExportAsFixedFormat(
XlFixedFormatType.xlTypePDF,
string.Format("{0}\\Factuur {1}{2:D4}.pdf", savelocation, DateTime.Now.Year, orderid),
XlFixedFormatQuality.xlQualityStandard, true, false, Type.Missing, Type.Missing, false
);
oBook.Close();
oBook = null;
oExcel.Quit();
oExcel = null;
Marshal.ReleaseComObject(oSheet);
GC.Collect();
GC.WaitForPendingFinalizers();
は、私も試してみると、タスクマネージャでプロセスを残し取り除くガベージコレクタとマーシャルに関するいくつかの行を持っているが、それは動作していないようです。
また、このコードは最も効率的ではありません。私はちょうど基本的なことを働かせたい。
EDIT:残念ながら
https://drive.google.com/open?id=0B6hUijkN5GM3QmZ3YkdNd1l0ejg
です。この問題に関して私が見たすべての答えは、 'FitToPagesWide/Tall'を設定する前に' PageSetup.Zoom = false; '*を設定しています。私はVSTOやCOMの専門家ではありませんが、後で呼び出すのではなく、プロパティが設定されているときにプロパティの設定がプロパティをチェックすると、問題が発生することがわかりました。 – Quantic
@Quantic違いはありませんでしたが、ありがとうございました。 – Thodor12
私はあなたのファイルとあなたのコードをテストしていて、問題を再現することはできません。私には1ページ分のpdfが保存されます。 'order'、' customer'、 'shipping'、' orderid'などの全ての文字列をランダムな文字列や数字に置き換えています。また、私はExcel 2010でテストしています。 – Quantic