当前位置:首页
开发技术指南» 文章正文
    引言:

    摘要: cmyclass是我自己定义的类 cmyclass* pcmyclass = new cmyclass[num]; 我这样申请后 怎么使用分配的每个对象 直接 pcmyclass-> pcmyclass++ ? ......
 ·如何使用winpcap的api函数    »显示摘要«
    摘要: 我按照网上的教程,已经包含了这两个文件了,但是为什么还是一堆错误?提示这些api没有定义,还有 \packet32.h(208) : error c2079: ipaddress uses undefined struct sockaddr_storage \packet32.h(209) : error c2079: subnetmask uses undefined struct so......


如何在 c# 中设置excel 的某一列为文本,以及他的背景颜色

如何在   c#   中设置excel     的某一列为文本,以及他的背景颜色,文字的对齐方式  
   
  当把数据从应用程序中导入到excel的时候,经常有这种现象  
   
  应用程序中的字符串“6951-1”到了excel里面就成了“Jan-51”  
   
  请有经验的师兄师弟们给出宝贵的代码片断,不胜感激。  
   
   
 

NO.1   作者: weisunding

在数字型字符串前面加入一个撇!  
   
  cell[0,0]   =   "6951-1";

NO.2   作者: looner

using   System;  
  using   System.Data;  
  using   System.Drawing;  
  using   Excel;  
  using   System.Reflection;  
  using   Me.DataBase;  
   
  namespace   Me.ExcelAPP  
  {  
  ///   <summary>  
  ///   OperateExcel   篕璶磞瓃  
  ///   </summary>  
  public   class   OperateExcel  
  {  
  public   OperateExcel()  
  {  
  //  
  //   TODO:   篶ㄧΑ祘Α絏  
  //  
  }  
   
   
  public   void   ExportToExcelFile(string   strFilePath,System.Data.DataTable   dt,string   strServerName,DateTime   sysdate)  
  {  
   
  int   i=0;  
  DateTime   excelDate=DateTime.Now.AddYears(-1);  
  const   int   START_ROW=5;  
  int   intExcelID=0,intRanColorIndex=0;  
  string[,]   sData=new   string[dt.Rows.Count,8];  
  TimeSpan   ts=new   TimeSpan();  
  Color   colorRanBack=new   Color();  
  Excel.ApplicationClass   xApp   =   new   Excel.ApplicationClass();  
  Excel.Range   xRan;  
  //ъ讽玡秨币Excel郎秈祘ID玂Kill非称  
  foreach   (System.Diagnostics.Process   excelProc   in   System.Diagnostics.Process.GetProcessesByName("EXCEL"))  
  {  
  //硄筁逼矪瞶玂谍讽╰参いΤExcel秈祘  
  //ъご琌讽玡祘Α秨币Excel秈祘StartTime程  
  if   (excelProc.StartTime>excelDate)  
  {  
  excelDate=excelProc.StartTime   ;  
  intExcelID=excelProc.Id;  
  }  
  }  
  Excel._Workbook   xBook   =   xApp.Workbooks.Open(System.IO.Directory.GetCurrentDirectory()   +   @"\MonitorAlarm.xls",  
  Missing.Value,Missing.Value,Missing.Value,Missing.Value  
  ,Missing.Value,Missing.Value,Missing.Value,Missing.Value  
  ,Missing.Value,Missing.Value,Missing.Value,Missing.Value);    
  Excel._Worksheet   xSheet   =   (_Worksheet)   (xBook.Worksheets.get_Item(1));  
  xApp.Visible=false;  
   
  //计沮矪瞶砰场だ  
  try  
  {  
  xSheet.Cells[2,1]="Server   Name:   "   +   strServerName;  
  xSheet.Cells[3,1]="Report   Time:   "   +   sysdate.ToString("yyyy/MM/dd   HH:mm:ss");  
  for(i=0;i<dt.Rows.Count;i++)  
  {  
  sData[i,0]=Convert.ToString(i+1);  
  sData[i,1]=dt.Rows[i]["IP"].ToString();  
  sData[i,2]=dt.Rows[i]["AP_NAME"].ToString();  
  sData[i,3]=dt.Rows[i]["FUNCTION_NAME"].ToString();  
  sData[i,4]=dt.Rows[i]["AUTORUN_TIME"].ToString()   +   "   (minutes)";  
   
   
  if   (dt.Rows[i]["WORK_TIME"].ToString()=="")  
  { //讽Rい礚计沮眖ゼ笲︽筁  
  sData[i,5]="Never   Run";  
  intRanColorIndex=6;//独︹  
  sData[i,6]="";  
  sData[i,7]="";  
  }  
  else  
  {  
  sData[i,6]=Convert.ToDateTime(dt.Rows[i]["WORK_TIME"]).ToString("yyyy/MM/dd   HH:mm:ss");  
  ts=   sysdate-Convert.ToDateTime(dt.Rows[i]["WORK_TIME"]);  
   
  if   (ts.TotalMinutes>Convert.ToSingle(dt.Rows[i]["AUTORUN_TIME"].ToString())*1.5)  
  {  
  sData[i,5]="Miss   Sign";  
  intRanColorIndex=3;//︹  
  sData[i,7]=ts.TotalMinutes.ToString()   ;  
  }  
  else  
  {  
  if   (dt.Rows[i]["WORK_FLAG"].ToString()=="0")  
  {  
  sData[i,5]="Well   Run";  
  intRanColorIndex=4;//睱厚︹  
  sData[i,7]="";  
  }  
  else  
  {  
  sData[i,5]="Fail   Run";  
  intRanColorIndex=46;//卷︹  
  sData[i,7]=ts.TotalMinutes.ToString();  
  }  
  }  
  }  
  xRan=xApp.get_Range("A"+(START_ROW+i).ToString(),"H"   +   (START_ROW+i).ToString());  
  xRan.Interior.ColorIndex=intRanColorIndex;  
   
  xRan.Interior.Pattern=Excel.Constants.xlSolid;  
  //xSheet.Rows[START_ROW].  
   
   
  //xApp.Rows   xRow   ;  
   
  //xApp.Selection.Interior.ColorIndex   =   6;  
  //xApp.Selection.Interior.Pattern   =   Excel.Constants.xlSolid;  
  }  
   
   
   
   
   
  //恶计沮  
  xRan=xApp.get_Range("A"+START_ROW.ToString(),"H"   +   Convert.ToString(START_ROW   +   dt.Rows.Count-1));  
  xRan.Value=sData;  
   
   
  xApp.AlertBeforeOverwriting=false;  
  xApp.DisplayAlerts=false;  
  xApp.EnableAutoComplete=true;  
   
  xApp.ActiveWorkbook.SaveAs(strFilePath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value    
  ,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value  
  ,Missing.Value);  
   
  //-----------﹍睦Excel癸禜絏-------------------------  
  //  
  // 琌硄筁Kill秈祘よΑ癶Excel  
  // ┮硂琿絏ぃ  
  // 眖祘Α呸胯腨略┦σ納某  
  xRan=null;  
  xSheet=null;  
  xBook.Close(Missing.Value,Missing.Value,Missing.Value);  
  xBook=null;  
  xApp.Workbooks.Close();  
  xApp.Quit();  
  xApp=null;  
  //-------------------------------------------------------------  
   
  //奔ノKill   Excel秈祘家舱  
  KillExcelByID(intExcelID);  
   
  //ㄢ.NET矗ㄑ︰ВΜ诀ヘ玡ぃぃ穦紇臫狦  
  //τ耕╰参戈方珿猔睦奔ぃ睰  
  //GC.Collect();  
  //GC.WaitForPendingFinalizers();  
   
  }  
  catch  
  {  
  KillExcelByID(intExcelID);  
  throw;  
  }  
  }  
   
   
   
  private   void   KillExcel(DateTime   startTime)  
  {  
  foreach   (System.Diagnostics.Process   excelProc   in   System.Diagnostics.Process.GetProcessesByName("EXCEL"))  
  {  
  TimeSpan   ts=excelProc.StartTime-startTime;  
  if   (Math.Abs(ts.TotalMilliseconds)<1000)  
  {  
  excelProc.Kill();  
  }  
  }  
  }  
   
  private   void   KillExcelByID(int   intExcelID)  
  {  
  foreach   (System.Diagnostics.Process   excelProc   in   System.Diagnostics.Process.GetProcessesByName("EXCEL"))  
  {  
  if   (excelProc.Id==intExcelID)  
  {  
  excelProc.Kill();  
  }  
  }  
  }  
   
  }  
   
  }  
 

NO.3   作者: looner

Excel里的Color类型和.net里的Color类型不一样,所以只能通过index来赋值,至于如何得到某种颜色的index是多少,只能在Excel里录巨集来得到了:(


    摘要: 谁有中国内地身份证验证存储过程(包括15位和18位)? 现有表sfz,要求查出所有不合法的身份证的所有数据。 我想一执行这个存储过程就能查出所有不合法身份证的所有数据。 1。长度必须为15位或18位。 2。身份证里面生成的出生日期要正确。 3。长度为18位的最后一位校验码要正确。 4。前面两位的地址码必须正确。 ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE