owc10导出excell

public static bool ExportForData(System.Data.DataTable[] arr, string fileName, bool isShowExcle)
{

OWC10.SpreadsheetClass oSheet = new OWC10.SpreadsheetClass();

OWC10.Workbook book = oSheet.ActiveWorkbook;
OWC10.Worksheets sheets = book.Worksheets;


int arrLen = arr.Length;
for (int i = 0; i < arrLen; i++) {
System.Data.DataTable dt = arr[i];
OWC10.Worksheet worksheet = (OWC10.Worksheet)sheets.Add(Missing.Value, Missing.Value, 1, Missing.Value);
if (worksheet == null) {
return false;
}
//worksheet.Name = dt.TableName;
worksheet.Name = arr[i].TableName;

//标题
int cl = 0;
System.Data.DataColumnCollection dcc = dt.Columns;

foreach (System.Data.DataColumn dc in dcc) {
worksheet.Cells[1, 1 + cl] = dc.Caption;
if (dc.Caption.Equals("允许空") || dc.Caption.Equals("主键") || dc.Caption.Equals("外键") || dc.Caption.Equals("是否标志") || dc.Caption.Equals("是否公式")) {
worksheet.get_Range(worksheet.Cells[1, 1 + cl], worksheet.Cells[1, 1 + cl]).set_ColumnWidth(8);
}
else {
worksheet.get_Range(worksheet.Cells[1, 1 + cl], worksheet.Cells[1, 1 + cl]).set_ColumnWidth(11);
}

cl++;
}


worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, dt.Columns.Count]).Font.set_Bold(true);
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, dt.Columns.Count]).Interior.set_ColorIndex(15);//背景色设置


//数据
int rowCount = dt.Rows.Count;
int colCount = dt.Columns.Count;
for (int r = 0; r < rowCount; r++) {
for (int l = 0; l < colCount; l++) {

if (dt.Rows[r][l].ToString().ToLower().Equals("false")) {
worksheet.Cells[r + 2, l + 1] = "否";
}
else if (dt.Rows[r][l].ToString().ToLower().Equals("true")) {
worksheet.Cells[r + 2, l + 1] = "是";
}
else {

worksheet.Cells[r + 2, l + 1] = dt.Rows[r][l].ToString();
}

}

}
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[dt.Rows.Count + 1, dt.Columns.Count]).Borders.set_LineStyle(OWC10.XlLineStyle.xlContinuous);

}

oSheet.Export(fileName, OWC10.SheetExportActionEnum.ssExportActionOpenInExcel, OWC10.SheetExportFormat.ssExportAsAppropriate);
return true;

}

你可能感兴趣的:(导出Excel)