DataTable dataTableID = select.GetPicFileByPid("FAI_WB");
if (dataTableID.Rows.Count > 0)
{
byte[] fileBytes = (byte[])dataTableID.Rows[0]["PIC_CONTENT"];
//将服务器模板下载到根目录文件夹
string filePath = Application.StartupPath + @"\template\";
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
if (System.IO.File.Exists(filePath + dataTableID.Rows[0]["PIC_NAME"]))
System.IO.File.Delete(filePath + dataTableID.Rows[0]["PIC_NAME"]);
FileStream fs = new FileStream(filePath + dataTableID.Rows[0]["PIC_NAME"], FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(fileBytes, 0, fileBytes.Length);
bw.Close();
fs.Close();
}
//读取报表模板,填入数据
string path = Application.StartupPath + "\\template\\" + dataTableID.Rows[0]["PIC_NAME"].ToString();
Workbook workbook = new Workbook(path); //创建Aspose.Cells工作对象
//创建一个workbookdesigner对象
WorkbookDesigner designer = new WorkbookDesigner(workbook);
设置实体类对象
designer.SetDataSource(dt);
var sheet = designer.Workbook.Worksheets[0];
sheet.Cells["C2"].PutValue(textBox1.Text);
sheet.Cells["E2"].PutValue(textBox2.Text);
/*
对模板指定单元格赋值
*/
//根据数据源处理生成报表内容
designer.Process();
//保存Excel文件
string fileToSave = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\检查表-" + textBox7.Text + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
if (File.Exists(fileToSave))
{
File.Delete(fileToSave);
}
designer.Workbook.Save(fileToSave);
打开Excel文件
//Process.Start(fileToSave);
MSG.ShowInfo(utility.GlobalClientLang, "保存成功");