Asp.Net用OWC操作Excel的实例代码
网络编程 2021-07-04 22:40www.168986.cn编程入门
这篇文章介绍了Asp.Net用OWC操作Excel的实例代码,有需要的朋友可以参考一下,希望对你有所帮助
代码如下:
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
{
}
}
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程