这篇文章介绍了Asp
Net用OWC操作Excel的实例代码
有需要的朋友可以参考一下
希望对你有所帮助
复制代码 代码如下:
string connstr = SystemConfigurationConfigurationManagerConnectionStrings["DqpiHrConnectionString"]ToString();
SqlConnection conn = new SqlConnection(connstr);
SqlDataAdapter sda = new SqlDataAdapter(sqlText conn);
DataSet ds = new DataSet();
connOpen();
sdaFill(ds);
connClose();
OWCSpreadsheetClass xlsheet;
xlsheet= new OWCSpreadsheetClass();
DataRow dr;
int i = ;
for(int ii=;ii<dsTables[]RowsCount;ii++)
{
dr = dsTables[]Rows[ii];
//合并单元格
xlsheetget_Range(xlsheetCells[i+ ] xlsheetCells[i+ ])set_MergeCells(true);
xlsheetget_Range(xlsheetCells[i + ] xlsheetCells[i + ])set_MergeCells(true);
xlsheetget_Range(xlsheetCells[i + ] xlsheetCells[i + ])set_MergeCells(true);
xlsheetget_Range(xlsheetCells[i + ] xlsheetCells[i + ])set_MergeCells(true);
xlsheetActiveSheetCells[i + ] = dr["姓名"]ToString() + "自然情况";
//字体加粗
xlsheetget_Range(xlsheetCells[i + ] xlsheetCells[i + ])Fontset_Bold(true);
//单元格文本水平居中对齐
xlsheetget_Range(xlsheetCells[i + ] xlsheetCells[i + ])set_HorizontalAlignment(OWCXlHAlignxlHAlignCenter);
//设置字体大小
xlsheetget_Range(xlsheetCells[i + ] xlsheetCells[i + ])Fontset_Size();
//设置列宽
xlsheetget_Range(xlsheetCells[i + ] xlsheetCells[i + ])set_ColumnWidth();
//画边框线
xlsheetget_Range(xlsheetCells[i + ] xlsheetCells[i+ ])Bordersset_LineStyle(OWCXlLineStylexlContinuous);
//写入数据 (这里由DS生成)
xlsheetActiveSheetCells[i + ] = "姓名";
xlsheetActiveSheetCells[i + ] = dr["姓名"]ToString();
xlsheetActiveSheetCells[i + ] = "曾用名";
xlsheetActiveSheetCells[i + ] = dr["曾用名"]ToString();
xlsheetActiveSheetCells[i + ] = "出生年月";
xlsheetActiveSheetCells[i + ] = DateTimeParse(dr["出生年月"]ToString())YearToString() + "" + DateTimeParse(dr["出生年月"]ToString())MonthToString();
xlsheetActiveSheetCells[i + ] = " 参加工作时间";
xlsheetActiveSheetCells[i + ] = DateTimeParse(dr["参加工作时间"]ToString())YearToString() + "" + DateTimeParse(dr["参加工作时间"]ToString())MonthToString();
xlsheetActiveSheetCells[i + ] = "性别";
xlsheetActiveSheetCells[i + ] = dr["性别"]ToString();
xlsheetActiveSheetCells[i + ] = "民族";
xlsheetActiveSheetCells[i + ] = dr["民族"]ToString();
xlsheetActiveSheetCells[i + ] = "政治面貌";
xlsheetActiveSheetCells[i + ] = dr["政治面貌"]ToString();
xlsheetActiveSheetCells[i + ] = "职称";
xlsheetActiveSheetCells[i + ] = dr["职称"]ToString();
xlsheetActiveSheetCells[i + ] = "学历";
xlsheetActiveSheetCells[i + ] = dr["学历"]ToString();
xlsheetActiveSheetCells[i + ] = "学位";
xlsheetActiveSheetCells[i + ] = dr["学位"]ToString();
xlsheetActiveSheetCells[i + ] = "职务";
xlsheetActiveSheetCells[i + ] = dr["职务"]ToString();
xlsheetActiveSheetCells[i + ] = "档案号码";
//Excel不支持开头输入加上姓氏首字母正好是编号全称
xlsheetActiveSheetCells[i + ] = dr["姓氏首字母"]ToString() + dr["档案号码"]ToString();
xlsheetActiveSheetCells[i + ] = "现从事专业" + dr["现从事专业"]ToString();
xlsheetActiveSheetCells[i + ] = "工作单位" + dr["工作单位"]ToString();
xlsheetActiveSheetCells[i + ] = "身份证" + dr["身份证号"]ToString();
i += ;
}
try
{
string D = DateTimeNowYearToString() + DateTimeNowMonthToString() + DateTimeNowDayToString() +
DateTimeNowHourToString() + DateTimeNowMinuteToString() + DateTimeNowSecondToString()+
DateTimeNowMillisecondToString();
xlsheetExport(ServerMapPath("/")+""+D+"xls" OWCSheetExportActionEnumssExportActionNone OWCSheetExportFormatssExportXMLSpreadsheet);
ResponseWrite("<script>windowopen("+D+"xls)</script>");
}
catch
{
}
}