Asp.Net用OWC操作Excel的实例代码
程序员文章站
2024-03-01 20:25:22
复制代码 代码如下: string connstr = system.configuration.configurationmanage...
复制代码 代码如下:
string connstr = system.configuration.configurationmanager.connectionstrings["dqpihrconnectionstring"].tostring();
sqlconnection conn = new sqlconnection(connstr);
sqldataadapter sda = new sqldataadapter(sql1.text, conn);
dataset ds = new dataset();
conn.open();
sda.fill(ds);
conn.close();
owc10.spreadsheetclass xlsheet;
xlsheet= new owc10.spreadsheetclass();
datarow dr;
int i = 0;
for(int ii=0;ii<ds.tables[0].rows.count;ii++)
{
dr = ds.tables[0].rows[ii];
//合并单元格
xlsheet.get_range(xlsheet.cells[i+1, 1], xlsheet.cells[i+1, 8]).set_mergecells(true);
xlsheet.get_range(xlsheet.cells[i + 5, 1], xlsheet.cells[i + 5, 3]).set_mergecells(true);
xlsheet.get_range(xlsheet.cells[i + 5, 4], xlsheet.cells[i + 5, 6]).set_mergecells(true);
xlsheet.get_range(xlsheet.cells[i + 5, 7], xlsheet.cells[i + 5, 8]).set_mergecells(true);
xlsheet.activesheet.cells[i + 1, 1] = dr["姓名"].tostring() + "自然情况";
//字体加粗
xlsheet.get_range(xlsheet.cells[i + 1, 1], xlsheet.cells[i + 1, 14]).font.set_bold(true);
//单元格文本水平居中对齐
xlsheet.get_range(xlsheet.cells[i + 1, 1], xlsheet.cells[i + 1, 14]).set_horizontalalignment(owc10.xlhalign.xlhaligncenter);
//设置字体大小
xlsheet.get_range(xlsheet.cells[i + 1, 1], xlsheet.cells[i + 1, 14]).font.set_size(14);
//设置列宽
xlsheet.get_range(xlsheet.cells[i + 1, 8], xlsheet.cells[i + 1, 8]).set_columnwidth(20);
//画边框线
xlsheet.get_range(xlsheet.cells[i + 1, 1], xlsheet.cells[i+5, 8]).borders.set_linestyle(owc10.xllinestyle.xlcontinuous);
//写入数据 (这里由ds生成)
xlsheet.activesheet.cells[i + 2, 1] = "姓名";
xlsheet.activesheet.cells[i + 2, 2] = dr["姓名"].tostring();
xlsheet.activesheet.cells[i + 2, 3] = "曾用名";
xlsheet.activesheet.cells[i + 2, 4] = dr["曾用名"].tostring();
xlsheet.activesheet.cells[i + 2, 5] = "出生年月";
xlsheet.activesheet.cells[i + 2, 6] = datetime.parse(dr["出生年月"].tostring()).year.tostring() + "-" + datetime.parse(dr["出生年月"].tostring()).month.tostring();
xlsheet.activesheet.cells[i + 2, 7] = " 参加工作时间";
xlsheet.activesheet.cells[i + 2, 8] = datetime.parse(dr["参加工作时间"].tostring()).year.tostring() + "-" + datetime.parse(dr["参加工作时间"].tostring()).month.tostring();
xlsheet.activesheet.cells[i + 3, 1] = "性别";
xlsheet.activesheet.cells[i + 3, 2] = dr["性别"].tostring();
xlsheet.activesheet.cells[i + 3, 3] = "民族";
xlsheet.activesheet.cells[i + 3, 4] = dr["民族"].tostring();
xlsheet.activesheet.cells[i + 3, 5] = "政治面貌";
xlsheet.activesheet.cells[i + 3, 6] = dr["政治面貌"].tostring();
xlsheet.activesheet.cells[i + 3, 7] = "职称";
xlsheet.activesheet.cells[i + 3, 8] = dr["职称"].tostring();
xlsheet.activesheet.cells[i + 4, 1] = "学历";
xlsheet.activesheet.cells[i + 4, 2] = dr["学历"].tostring();
xlsheet.activesheet.cells[i + 4, 3] = "学位";
xlsheet.activesheet.cells[i + 4, 4] = dr["学位"].tostring();
xlsheet.activesheet.cells[i + 4, 5] = "职务";
xlsheet.activesheet.cells[i + 4, 6] = dr["职务"].tostring();
xlsheet.activesheet.cells[i + 4, 7] = "档案号码";
//excel不支持0开头输入,加上姓氏首字母正好是编号全称
xlsheet.activesheet.cells[i + 4, 8] = dr["姓氏首字母"].tostring() + dr["档案号码"].tostring();
xlsheet.activesheet.cells[i + 5, 1] = "现从事专业:" + dr["现从事专业"].tostring();
xlsheet.activesheet.cells[i + 5, 4] = "工作单位:" + dr["工作单位"].tostring();
xlsheet.activesheet.cells[i + 5, 7] = "身份证:" + dr["身份证号"].tostring();
i += 6;
}
try
{
string d = datetime.now.year.tostring() + datetime.now.month.tostring() + datetime.now.day.tostring() +
datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.second.tostring()+
datetime.now.millisecond.tostring();
xlsheet.export(server.mappath("./")+"\\"+d+".xls", owc10.sheetexportactionenum.ssexportactionnone, owc10.sheetexportformat.ssexportxmlspreadsheet);
response.write("<script>window.open('"+d+".xls')</script>");
}
catch
{
}
}
下一篇: 用CSS实现表单form布局