刚刚做了一个winform的打印预览功能,用的是Micorsoft.Office.Interop.Excel。打印预览的时候显示不全。有11列,预览的时候只有10列。不知道怎么回事。求助各位大神一下。以下是代码
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
}
bool fileSaved = false;
string strpath = Application.StartupPath + "\\template\\Test.xlsx";
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel._Workbook workbook = workbooks.Add(strpath); //加载模板
Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Sheets;
Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(1); //第一个工作薄
//var mark = new JObject { { "序号", "1" }, { "所属科室", "一级库" },{ "注册证产品名", "止血器" },
// { "简称", "止血器" }, { "规格", "Z6" }, { "包装单位", "一级库" },{ "单位数量", "止血器" },
// { "总数量", "6" }, { "失效日期", "2019-09-01" },{ "注册证号", "国械注进20173770210" },{ "注册证有效期", "2020-08-20" }};
string str = "{\"id\":1,\"Suoshu\":\"一级库\",\"ReditName\":\"止血器\",\"Name\":\"止血器\",\"Guige\":\"Z6\",\"Company\":\"一级库\",\"Count\":\"9\",\"Sum\":\"5\",\"sDate\":\"2019-09-01\",\"IDhao\":\"国械注进20173770210\",\"yDate\":\"2019-09-01\"}";
YLHCMessage ylhc = JsonConvert.DeserializeObject<YLHCMessage>(str);
worksheet.Cells[4, 1] = ylhc.id.ToString();
worksheet.Cells[4, 2] = ylhc.Suoshu.ToString();
worksheet.Cells[4, 3] = ylhc.ReditName.ToString();
worksheet.Cells[4, 4] = ylhc.Name.ToString();
worksheet.Cells[4, 5] = ylhc.Guige.ToString();
worksheet.Cells[4, 6] = ylhc.Company.ToString();
worksheet.Cells[4, 7] = ylhc.Count;
worksheet.Cells[4, 8] = ylhc.Sum;
worksheet.Cells[4, 9] = Convert.ToDateTime(ylhc.sDate.ToString());
worksheet.Cells[4, 10] = ylhc.IDhao.ToString();
worksheet.Cells[4, 11] = Convert.ToDateTime(ylhc.yDate.ToString());
string savaPath = @"C:\Users\dell、\Desktop_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
workbook.SaveAs(savaPath);
workbooks.Open(savaPath);
xlApp.Visible = true;
workbook.PrintPreview();
workbook.Save();
xlApp.Quit();
workbooks.Close();
|