這篇文章介紹了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
{
}
}
From:http://tw.wingwit.com/Article/program/net/201311/14401.html