ExcelIOコンポーネントを使用しサーバーサイドのみでExcel-PDF変換を行うと例外が発生する
対象製品
SpreadJS 9J
発生環境
9.20171.0
状況
回避方法あり
詳細
クライアント側でのSpreadJS表示を行わず、ExcelIOコンポーネントを使用しサーバーサイドのみでExcel-PDF変換を行うと例外が発生する場合があります。
【再現手順】
1.サーバーサイドでExcelIOコンポーネントを使用し、以下の再現コードを実行します
【動作結果】
例外が発生します
【再現コード】
using (MemoryStream memstream = new MemoryStream())
{
// エクセルファイルをインポートします
string jsonData = new Importer().ImportExcel(new MemoryStream(System.IO.File.ReadAllBytes(/*任意のエクセルファイル*/))));
var excelEx = new Exporter(jsonData);
// インポートしたエクセルファイルのデータをPDF形式で保存します
excelEx.SavePdf(memstream, new GrapeCity.Windows.SpreadSheet.Data.PdfExportSettings(), 0);
return File(memstream.ToArray(), "application/pdf", "sample.pdf");
}
【再現手順】
1.サーバーサイドでExcelIOコンポーネントを使用し、以下の再現コードを実行します
【動作結果】
例外が発生します
【再現コード】
using (MemoryStream memstream = new MemoryStream())
{
// エクセルファイルをインポートします
string jsonData = new Importer().ImportExcel(new MemoryStream(System.IO.File.ReadAllBytes(/*任意のエクセルファイル*/))));
var excelEx = new Exporter(jsonData);
// インポートしたエクセルファイルのデータをPDF形式で保存します
excelEx.SavePdf(memstream, new GrapeCity.Windows.SpreadSheet.Data.PdfExportSettings(), 0);
return File(memstream.ToArray(), "application/pdf", "sample.pdf");
}
回避方法
本現象は、インポート対象となるエクセルファイルが印刷情報としてヘッダやフッタ情報を保有し、これらのフォント設定が"レギュラー"などの日本語表記である場合に発生します。
以下のようにしてインポート・エクスポート時にこれらの情報を英語情報に変換することで、現象の回避が可能です。
//フォント情報変換関数
private string ConvertJPFontStyleToEn(string font)
{
string result = font.Replace("レギュラー", "Regular").Replace("斜体", "Italic").Replace("太字", "Bold");
return result;
}
【再現コードへの回避コード使用例】
using (MemoryStream memstream = new MemoryStream())
{
string jsonData = new Importer().ImportExcel(new MemoryStream(System.IO.File.ReadAllBytes(/*任意のエクセルファイル*/))));
JObject spread = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonData);
foreach (JProperty sheet in spread["sheets"].Children())
{
JObject printInfo = (JObject)sheet.Value["printInfo"];
if (printInfo != null) {
printInfo["headerCenter"] = ConvertJPFontStyleToEn(printInfo["headerCenter"].ToString());
printInfo["headerLeft"] = ConvertJPFontStyleToEn(printInfo["headerLeft"].ToString());
printInfo["headerRight"] = ConvertJPFontStyleToEn(printInfo["headerRight"].ToString());
printInfo["footerCenter"] = ConvertJPFontStyleToEn(printInfo["footerCenter"].ToString());
printInfo["footerLeft"] = ConvertJPFontStyleToEn(printInfo["footerLeft"].ToString());
printInfo["footerRight"] = ConvertJPFontStyleToEn(printInfo["footerRight"].ToString());
}
}
var excelEx = new Exporter(spread);
excelEx.SavePdf(memstream, new GrapeCity.Windows.SpreadSheet.Data.PdfExportSettings(), 0);
return File(memstream.ToArray(), "application/pdf", "sample.pdf");
}
以下のようにしてインポート・エクスポート時にこれらの情報を英語情報に変換することで、現象の回避が可能です。
//フォント情報変換関数
private string ConvertJPFontStyleToEn(string font)
{
string result = font.Replace("レギュラー", "Regular").Replace("斜体", "Italic").Replace("太字", "Bold");
return result;
}
【再現コードへの回避コード使用例】
using (MemoryStream memstream = new MemoryStream())
{
string jsonData = new Importer().ImportExcel(new MemoryStream(System.IO.File.ReadAllBytes(/*任意のエクセルファイル*/))));
JObject spread = Newtonsoft.Json.JsonConvert.DeserializeObject
foreach (JProperty sheet in spread["sheets"].Children())
{
JObject printInfo = (JObject)sheet.Value["printInfo"];
if (printInfo != null) {
printInfo["headerCenter"] = ConvertJPFontStyleToEn(printInfo["headerCenter"].ToString());
printInfo["headerLeft"] = ConvertJPFontStyleToEn(printInfo["headerLeft"].ToString());
printInfo["headerRight"] = ConvertJPFontStyleToEn(printInfo["headerRight"].ToString());
printInfo["footerCenter"] = ConvertJPFontStyleToEn(printInfo["footerCenter"].ToString());
printInfo["footerLeft"] = ConvertJPFontStyleToEn(printInfo["footerLeft"].ToString());
printInfo["footerRight"] = ConvertJPFontStyleToEn(printInfo["footerRight"].ToString());
}
}
var excelEx = new Exporter(spread);
excelEx.SavePdf(memstream, new GrapeCity.Windows.SpreadSheet.Data.PdfExportSettings(), 0);
return File(memstream.ToArray(), "application/pdf", "sample.pdf");
}