#region 右擊文件 屬性信息{DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();dsi.Company = "http://www.openwebgis.com/";workbook.DocumentSummaryInformation = dsi;SummaryInformation si = PropertySetFactory.CreateSummaryInformation();si.Author = "OPENWEBGIS"; //填加xls文件作者信息si.ApplicationName = "OPENWEBGIS EXCEL EXPORT"; //填加xls文件創(chuàng)建程序信息si.LastAuthor = "LIQUIDGIS"; //填加xls文件最后保存者信息si.Comments = "版權(quán)所有 (C) 2007-2013 OpenWebGIS All Rights Reserved "; //填加xls文件作者信息si.Title = "OPENWEBGIS EXCEL 文件導(dǎo)出"; //填加xls文件標(biāo)題信息si.Subject = "OPENWEBGIS EXCEL"; //填加文件主題信息si.CreateDateTime = DateTime.Now;workbook.SummaryInformation = si;}#endregionHSSFCellStyle dateStyle = workbook.CreateCellStyle() as HSSFCellStyle;HSSFDataFormat format = workbook.CreateDataFormat() as HSSFDataFormat;dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");//取得列寬int[] arrColWidth = new int[dtSource.Columns.Count];foreach (DataColumn item in dtSource.Columns){string caption = item.Caption;if (string.IsNullOrEmpty(caption)) caption = item.ColumnName;arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(caption).Length;}for (int i = 0; i < dtSource.Rows.Count; i++){for (int j = 0; j < dtSource.Columns.Count; j++){int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length;if (intTemp > arrColWidth[j]){arrColWidth[j] = intTemp;if (arrColWidth[j] > 250) arrColWidth[j] = 250;//The maximum column width for an individual cell is 255 characters}}}int rowIndex = 0;foreach (DataRow row in dtSource.Rows){#region 新建表,填充表頭,填充列頭,樣式if (rowIndex == 65535 || rowIndex == 0){if (rowIndex != 0){sheet = workbook.CreateSheet() as HSSFSheet;}#region 表頭及樣式{HSSFRow headerRow = sheet.CreateRow(0) as HSSFRow;headerRow.HeightInPoints = 25;headerRow.CreateCell(0).SetCellValue(strHeaderText);HSSFCellStyle headStyle = workbook.CreateCellStyle() as HSSFCellStyle;headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;HSSFFont font = workbook.CreateFont() as HSSFFont;font.FontHeightInPoints = 20;font.Boldweight = 700;headStyle.SetFont(font);headerRow.GetCell(0).CellStyle = headStyle;sheet.AddMergedRegion(new Region(0, 0, 0, dtSource.Columns.Count - 1));//headerRow.Dispose();}#endregion#region 列頭及樣式{HSSFRow headerRow = sheet.CreateRow(1) as HSSFRow;HSSFCellStyle headStyle = workbook.CreateCellStyle() as HSSFCellStyle;headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;HSSFFont font = workbook.CreateFont() as HSSFFont;font.FontHeightInPoints = 10;font.Boldweight = 700;headStyle.SetFont(font);foreach (DataColumn column in dtSource.Columns){string ColumnName = GetColnumnName(column.ColumnName);string caption = column.Caption;if (string.IsNullOrEmpty(ColumnName)) caption = column.ColumnName;elsecaption = ColumnName;headerRow.CreateCell(column.Ordinal).SetCellValue(caption);headerRow.GetCell(column.Ordinal).CellStyle = headStyle;//設(shè)置列寬sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);}//headerRow.Dispose();}#endregionrowIndex = 2;}#endregion#region 填充內(nèi)容HSSFRow dataRow = sheet.CreateRow(rowIndex) as HSSFRow;foreach (DataColumn column in dtSource.Columns){HSSFCell newCell = dataRow.CreateCell(column.Ordinal) as HSSFCell;string drValue = row[column].ToString();switch (column.DataType.ToString()){case "System.String": //字符串類型double result;if (isNumeric(drValue, out result)){double.TryParse(drValue, out result);newCell.SetCellValue(result);break;}else{newCell.SetCellValue(drValue);break;}case "System.DateTime": //日期類型DateTime dateV;DateTime.TryParse(drValue, out dateV);newCell.SetCellValue(dateV);newCell.CellStyle = dateStyle; //格式化顯示break;case "System.Boolean": //布爾型bool boolV = false;bool.TryParse(drValue, out boolV);newCell.SetCellValue(boolV);break;case "System.Int16": //整型case "System.Int32":case "System.Int64":case "System.Byte":int intV = 0;int.TryParse(drValue, out intV);newCell.SetCellValue(intV);break;case "System.Decimal": //浮點(diǎn)型case "System.Double":double doubV = 0;double.TryParse(drValue, out doubV);newCell.SetCellValue(doubV);break;case "System.DBNull": //空值處理newCell.SetCellValue("");break;default:newCell.SetCellValue("");break;}}#endregionrowIndex++;}using (MemoryStream ms = new MemoryStream()){workbook.Write(ms);ms.Flush();ms.Position = 0;//sheet;//workbook.Dispose();return ms;}
}
#retion word 動(dòng)態(tài)表格 public string SXYD(DocumentBuilder builder, string demp, string rowguid) { string sqlsx = “select t.*,a.TEMP_STAGE,a.MATTERNAME from app_relationtable t,app_model_matter a where t.matterguid in(select matterguid from app_model_matter ) and xmguid=’” + rowguid + “’ and a.matterguid=t.matterguid and t.bmmc=’” + demp + “’ and ( t.jsyd != ‘’ or t.SPYD !=’’)”; DataTable dtsx = DataService.SystemDatabase.GetDataTable(sqlsx); //設(shè)置各單元格的寬度 int w1 = 10; int w2 = 50; if (dtsx.Rows.Count > 0) {