class Sheets
{
public string 接件号, 名称, 型号规格, 检定员, 核验员;
}
protected void Button1_Click(object sender, EventArgs e)
{
Aspose.Cells.Workbook CurrentWorkbook;
Aspose.Cells.Worksheet DetailSheet;
//新建电子表格文件
CurrentWorkbook = new Aspose.Cells.Workbook();
// 打开第一个sheet
DetailSheet = CurrentWorkbook.Worksheets[0];
//设置标题样式
Aspose.Cells.Style style_title = new Aspose.Cells.Style();
style_title.Font.Size = 10;
style_title.Font.IsBold = true;
style_title.Font.Color = System.Drawing.Color.White;
style_title.ForegroundColor = System.Drawing.Color.Blue;
style_title.Pattern = Aspose.Cells.BackgroundType.Solid;
//设置正文样式
Aspose.Cells.Style style_content = new Aspose.Cells.Style();
style_content.Font.Size = 10;
style_content.Pattern = Aspose.Cells.BackgroundType.Solid;
style_content.ForegroundColor = System.Drawing.Color.Black;
//标题行
int row_num = 0, colomn_num = 0;
DetailSheet.Cells[0, colomn_num].PutValue("序号");
DetailSheet.Cells[0, colomn_num].SetStyle(style_title);
DetailSheet.Cells[0, ++colomn_num].PutValue("接件号");
DetailSheet.Cells[0, colomn_num].SetStyle(style_title);
DetailSheet.Cells[0, ++colomn_num].PutValue("被校设备名称");
DetailSheet.Cells[0, colomn_num].SetStyle(style_title);
DetailSheet.Cells[0, ++colomn_num].PutValue("型号规格");
DetailSheet.Cells[0, colomn_num].SetStyle(style_title);
DetailSheet.Cells[0, ++colomn_num].PutValue("校准人员");
DetailSheet.Cells[0, colomn_num].SetStyle(style_title);
DetailSheet.Cells[0, ++colomn_num].PutValue("校准方法");
DetailSheet.Cells[0, colomn_num].SetStyle(style_title);
DetailSheet.Cells[0, ++colomn_num].PutValue("标准设备名称");
DetailSheet.Cells[0, colomn_num].SetStyle(style_title);
DetailSheet.Cells[0, ++colomn_num].PutValue("标准设备型号规格");
DetailSheet.Cells[0, colomn_num].SetStyle(style_title);
DetailSheet.Cells[0, ++colomn_num].PutValue("溯源机构");
DetailSheet.Cells[0, colomn_num].SetStyle(style_title);
DetailSheet.Cells[0, ++colomn_num].PutValue("有效期至");
DetailSheet.Cells[0, colomn_num].SetStyle(style_title);
List
colomn_num=0;
string sql = @"SELECT 收发信息表.接件号, 收发信息表.名称, 收发信息表.型号规格,
工作人员表.姓名 as 检定员, 工作人员表_1.姓名 AS 核验员
FROM 收发信息表 INNER JOIN
工作人员表 ON 收发信息表.检定员 = 工作人员表.代码 INNER JOIN
工作人员表 AS 工作人员表_1 ON 收发信息表.核验员 = 工作人员表_1.代码
WHERE(收发信息表.电子副本 LIKE '%.docx') AND(收发信息表.电子副本pdf IS NOT NULL)
AND(收发信息表.接件时间 BETWEEN '2021-1-1' AND '2021-12-30') AND
(收发信息表.委托单位 LIKE '%可靠性%' OR 收发信息表.委托单位 LIKE '%检测校准%')
AND(收发信息表.委托部门 LIKE '检测%')
ORDER BY 收发信息表.接件号";
using (System.Data.SqlClient.SqlConnection con =
new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["local"].ToString()))
{
using (System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand(sql, con))
{
con.Open();
System.Data.SqlClient.SqlDataReader dr = com.ExecuteReader();
while (dr.Read() == true)
{
sheets.Add(new Sheets { 接件号=dr[0].ToString(), 名称 = dr[1].ToString(), 型号规格 = dr[2].ToString(), 检定员 = dr[3].ToString(), 核验员 = dr[4].ToString() });
}
}
}
int 序号 = 0;
foreach (var item in sheets)
{
colomn_num = 0;
row_num++;
DetailSheet.Cells[row_num, ++colomn_num].PutValue(item.接件号);
DetailSheet.Cells[row_num, ++colomn_num].PutValue(item.名称);
DetailSheet.Cells[row_num, ++colomn_num].PutValue(item.型号规格);
DetailSheet.Cells[row_num, ++colomn_num].PutValue(item.检定员+"、"+item.核验员);
int col = colomn_num++;
sql = "select 编号,名称 from _接件号_技术依据 where 接件号='" + item.接件号 + "'";
using (System.Data.SqlClient.SqlConnection con =
new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["local"].ToString()))
{
using (System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand(sql, con))
{
con.Open();
System.Data.SqlClient.SqlDataReader dr = com.ExecuteReader();
string temp = "";
if (dr.Read() == true) temp = dr[0].ToString() + " " + dr[1].ToString();
while (dr.Read() == true) temp += "\r\n" + dr[0].ToString() + " " + dr[1].ToString();//换行效果在文件打开后是一行,双击该单元格后会变为多行
DetailSheet.Cells[row_num, colomn_num].PutValue(temp);
}
}
colomn_num++;
col = colomn_num;
sql = "select 设备名称,型号规格,溯源机构,有效期至 from _接件号_标准器具 where 接件号='" + item.接件号+ "'";
using (System.Data.SqlClient.SqlConnection con =
new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["local"].ToString()))
{
using (System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand(sql, con))
{
int rows_added = 1;
con.Open();
System.Data.SqlClient.SqlDataReader dr = com.ExecuteReader();
if (dr.Read() == true)
{
colomn_num = col;
DetailSheet.Cells[row_num, colomn_num].PutValue(dr[0].ToString());
DetailSheet.Cells[row_num, ++colomn_num].PutValue(dr[1].ToString());
DetailSheet.Cells[row_num, ++colomn_num].PutValue(dr[2].ToString());
DetailSheet.Cells[row_num, ++colomn_num].PutValue(dr[3].ToString());
}
while (dr.Read() == true)
{
row_num++;
rows_added++;
colomn_num = col;
DetailSheet.Cells[row_num, colomn_num].PutValue(dr[0].ToString());
DetailSheet.Cells[row_num, ++colomn_num].PutValue(dr[1].ToString());
DetailSheet.Cells[row_num, ++colomn_num].PutValue(dr[2].ToString());
DetailSheet.Cells[row_num, ++colomn_num].PutValue(dr[3].ToString());
}
//合并列
序号++;
DetailSheet.Cells[row_num, 0].PutValue(序号.ToString());
if (rows_added > 1)
for (int i = 0; i < colomn_num - 3; i++)
DetailSheet.Cells.Merge(row_num - rows_added + 1, i, rows_added, 1);
}
}
}
DetailSheet.AutoFitColumns();
DetailSheet.AutoFitRows();
// 生成的文件名称
string ReportFileName = string.Format("校准设备清单_{0}.xlsx", DateTime.Now.ToString("yyyy-MM-dd"));
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("~/uploads/") + "/temp/");
if (di.Exists == false) di.Create();
// 保存文件
CurrentWorkbook.Save(di + "/" + ReportFileName, Aspose.Cells.SaveFormat.Xlsx);
Response.Redirect("~/uploads/temp/" + ReportFileName);
}
最终效果图