C#利用NOPI处理Excel的代码

http://blog.csdn.net/apollokk/article/details/8025611

http://download.csdn.net/detail/u010513245/6868749

  1. using System;  
  2. using System.Data;  
  3. using System.IO;  
  4. using System.Text;  
  5. using System.Web;  
  6. using NPOI.HPSF;  
  7. using NPOI.HSSF.UserModel;  
  8. using NPOI.SS.UserModel;  
  9.   
  10. public class ExcelHelper  
  11. {  
  12.     /// <summary>      
  13.     /// DataTable导出到Excel文件      
  14.     /// </summary>      
  15.     /// <param name="dtSource">源DataTable</param>      
  16.     /// <param name="strHeaderText">表头文本</param>      
  17.     /// <param name="strFileName">保存位置</param>   
  18.     /// <param name="strSheetName">工作表名称</param>   
  19.     /// <Author>柳永法 http://www.yongfa365.com/ 2010-5-8 22:21:41</Author>      
  20.     public static void Export(DataTable dtSource, string strHeaderText, string strFileName, string strSheetName, string[] oldColumnNames, string[] newColumnNames)  
  21.     {  
  22.         if (strSheetName == "")  
  23.         {  
  24.             strSheetName = "Sheet";  
  25.         }  
  26.         using (MemoryStream ms = Export(dtSource, strHeaderText, strSheetName, oldColumnNames, newColumnNames))  
  27.         {  
  28.             using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))  
  29.             {  
  30.                 byte[] data = ms.ToArray();  
  31.                 fs.Write(data, 0, data.Length);  
  32.                 fs.Flush();  
  33.             }  
  34.         }  
  35.     }  
  36.   
  37.     /// <summary>      
  38.     /// DataTable导出到Excel的MemoryStream      
  39.     /// </summary>      
  40.     /// <param name="dtSource">源DataTable</param>      
  41.     /// <param name="strHeaderText">表头文本</param>      
  42.     /// <param name="strSheetName">工作表名称</param>   
  43.     /// <Author>柳永法 http://www.yongfa365.com/ 2010-5-8 22:21:41</Author>      
  44.     public static MemoryStream Export(DataTable dtSource, string strHeaderText, string strSheetName, string[] oldColumnNames, string[] newColumnNames)  
  45.     {  
  46.         if (oldColumnNames.Length != newColumnNames.Length)  
  47.         {  
  48.             return new MemoryStream();  
  49.         }  
  50.         HSSFWorkbook workbook = new HSSFWorkbook();  
  51.         //HSSFSheet sheet = workbook.CreateSheet();// workbook.CreateSheet();   
  52.         ISheet sheet = workbook.CreateSheet(strSheetName);  
  53.  
  54.         #region 右击文件 属性信息   
  55.         {  
  56.             DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();  
  57.             dsi.Company = "http://....../";  
  58.             workbook.DocumentSummaryInformation = dsi;  
  59.   
  60.             SummaryInformation si = PropertySetFactory.CreateSummaryInformation();  
  61.             if (HttpContext.Current.Session["realname"] != null)  
  62.             {  
  63.                 si.Author = HttpContext.Current.Session["realname"].ToString();  
  64.             }  
  65.             else  
  66.             {  
  67.                 if (HttpContext.Current.Session["username"] != null)  
  68.                 {  
  69.                     si.Author = HttpContext.Current.Session["username"].ToString();  
  70.                 }  
  71.             }                                       //填加xls文件作者信息      
  72.             si.ApplicationName = "NPOI";            //填加xls文件创建程序信息      
  73.             si.LastAuthor = "OA系统";           //填加xls文件最后保存者信息      
  74.             si.Comments = "OA系统自动创建文件";      //填加xls文件作者信息      
  75.             si.Title = strHeaderText;               //填加xls文件标题信息      
  76.             si.Subject = strHeaderText;              //填加文件主题信息      
  77.             si.CreateDateTime = DateTime.Now;  
  78.             workbook.SummaryInformation = si;  
  79.         }  
  80.         #endregion   
  81.   
  82.         ICellStyle dateStyle = workbook.CreateCellStyle();  
  83.         IDataFormat format = workbook.CreateDataFormat();  
  84.         dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");  
  85.  
  86.         #region 取得列宽   
  87.         int[] arrColWidth = new int[oldColumnNames.Length];  
  88.         for (int i = 0; i < oldColumnNames.Length; i++)  
  89.         {  
  90.             arrColWidth[i] = Encoding.GetEncoding(936).GetBytes(newColumnNames[i]).Length;  
  91.         }  
  92.         /* 
  93.         foreach (DataColumn item in dtSource.Columns) 
  94.         { 
  95.             arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length; 
  96.         } 
  97.          * */  
  98.   
  99.         for (int i = 0; i < dtSource.Rows.Count; i++)  
  100.         {  
  101.             for (int j = 0; j < oldColumnNames.Length; j++)  
  102.             {  
  103.                 int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][oldColumnNames[j]].ToString()).Length;  
  104.                 if (intTemp > arrColWidth[j])  
  105.                 {  
  106.                     arrColWidth[j] = intTemp;  
  107.                 }  
  108.             }  
  109.             /* 
  110.             for (int j = 0; j < dtSource.Columns.Count; j++) 
  111.             { 
  112.                 int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length; 
  113.                 if (intTemp > arrColWidth[j]) 
  114.                 { 
  115.                     arrColWidth[j] = intTemp; 
  116.                 } 
  117.             } 
  118.              * */  
  119.         }  
  120.         #endregion   
  121.         int rowIndex = 0;  
  122.   
  123.         foreach (DataRow row in dtSource.Rows)  
  124.         {  
  125.             #region 新建表,填充表头,填充列头,样式   
  126.             if (rowIndex == 65535 || rowIndex == 0)  
  127.             {  
  128.                 if (rowIndex != 0)  
  129.                 {  
  130.                     sheet = workbook.CreateSheet(strSheetName + ((int)rowIndex/65535).ToString());  
  131.                 }  
  132.  
  133.                 #region 表头及样式   
  134.                 {  
  135.                     IRow headerRow = sheet.CreateRow(0);  
  136.                     headerRow.HeightInPoints = 25;  
  137.                     headerRow.CreateCell(0).SetCellValue(strHeaderText);  
  138.   
  139.                     ICellStyle headStyle = workbook.CreateCellStyle();  
  140.                     headStyle.Alignment = HorizontalAlignment.CENTER;  
  141.                     IFont font = workbook.CreateFont();  
  142.                     font.FontHeightInPoints = 20;  
  143.                     font.Boldweight = 700;  
  144.                     headStyle.SetFont(font);  
  145.   
  146.                     headerRow.GetCell(0).CellStyle = headStyle;  
  147.                     //sheet.AddMergedRegion(new Region(0, 0, 0, dtSource.Columns.Count - 1));   
  148.                     sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dtSource.Columns.Count - 1));  
  149.                 }  
  150.                 #endregion  
  151.  
  152.  
  153.                 #region 列头及样式   
  154.                 {  
  155.                     //HSSFRow headerRow = sheet.CreateRow(1);   
  156.                     IRow headerRow = sheet.CreateRow(1);  
  157.   
  158.                     ICellStyle headStyle = workbook.CreateCellStyle();  
  159.                     headStyle.Alignment = HorizontalAlignment.CENTER;  
  160.                     IFont font = workbook.CreateFont();  
  161.                     font.FontHeightInPoints = 10;  
  162.                     font.Boldweight = 700;  
  163.                     headStyle.SetFont(font);  
  164.   
  165.                     for (int i = 0; i < oldColumnNames.Length; i++)  
  166.                     {  
  167.                         headerRow.CreateCell(i).SetCellValue(newColumnNames[i]);  
  168.                         headerRow.GetCell(i).CellStyle = headStyle;  
  169.                         //设置列宽   
  170.                         sheet.SetColumnWidth(i, (arrColWidth[i] + 1) * 256);  
  171.                     }  
  172.                     /* 
  173.                     foreach (DataColumn column in dtSource.Columns) 
  174.                     { 
  175.                         headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); 
  176.                         headerRow.GetCell(column.Ordinal).CellStyle = headStyle; 
  177.  
  178.                         //设置列宽    
  179.                         sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); 
  180.                     } 
  181.                      * */  
  182.                 }  
  183.                 #endregion   
  184.   
  185.                 rowIndex = 2;  
  186.             }  
  187.             #endregion  
  188.  
  189.  
  190.             #region 填充内容   
  191.             IRow dataRow = sheet.CreateRow(rowIndex);  
  192.             //foreach (DataColumn column in dtSource.Columns)   
  193.             for(int i=0;i<oldColumnNames.Length;i++)  
  194.             {  
  195.                 ICell newCell = dataRow.CreateCell(i);  
  196.   
  197.                 string drValue = row[oldColumnNames[i]].ToString();  
  198.   
  199.                 switch (dtSource.Columns[oldColumnNames[i]].DataType.ToString())  
  200.                 {  
  201.                     case "System.String"://字符串类型      
  202.                         newCell.SetCellValue(drValue);  
  203.                         break;  
  204.                     case "System.DateTime"://日期类型      
  205.                         DateTime dateV;  
  206.                         DateTime.TryParse(drValue, out dateV);  
  207.                         newCell.SetCellValue(dateV);  
  208.   
  209.                         newCell.CellStyle = dateStyle;//格式化显示      
  210.                         break;  
  211.                     case "System.Boolean"://布尔型      
  212.                         bool boolV = false;  
  213.                         bool.TryParse(drValue, out boolV);  
  214.                         newCell.SetCellValue(boolV);  
  215.                         break;  
  216.                     case "System.Int16"://整型      
  217.                     case "System.Int32":  
  218.                     case "System.Int64":  
  219.                     case "System.Byte":  
  220.                         int intV = 0;  
  221.                         int.TryParse(drValue, out intV);  
  222.                         newCell.SetCellValue(intV);  
  223.                         break;  
  224.                     case "System.Decimal"://浮点型      
  225.                     case "System.Double":  
  226.                         double doubV = 0;  
  227.                         double.TryParse(drValue, out doubV);  
  228.                         newCell.SetCellValue(doubV);  
  229.                         break;  
  230.                     case "System.DBNull"://空值处理      
  231.                         newCell.SetCellValue("");  
  232.                         break;  
  233.                     default:  
  234.                         newCell.SetCellValue("");  
  235.                         break;  
  236.                 }  
  237.   
  238.             }  
  239.             #endregion   
  240.   
  241.             rowIndex++;  
  242.         }  
  243.   
  244.   
  245.         using (MemoryStream ms = new MemoryStream())  
  246.         {  
  247.             workbook.Write(ms);  
  248.             ms.Flush();  
  249.             ms.Position = 0;  
  250.   
  251.             //sheet.Dispose();   
  252.             sheet = null;  
  253.             workbook = null;  
  254.             //workbook.Dispose();//一般只用写这一个就OK了,他会遍历并释放所有资源,但当前版本有问题所以只释放sheet      
  255.             return ms;  
  256.         }  
  257.     }  
  258.   
  259.   
  260.     /// <summary>      
  261.     /// WEB导出DataTable到Excel      
  262.     /// </summary>      
  263.     /// <param name="dtSource">源DataTable</param>      
  264.     /// <param name="strHeaderText">表头文本</param>      
  265.     /// <param name="strFileName">文件名</param>      
  266.     /// <Author>柳永法 http://www.yongfa365.com/ 2010-5-8 22:21:41</Author>      
  267.     public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName)  
  268.     {  
  269.         ExportByWeb(dtSource, strHeaderText, strFileName, "sheet");  
  270.     }  
  271.   
  272.     /// <summary>   
  273.     /// WEB导出DataTable到Excel   
  274.     /// </summary>   
  275.     /// <param name="dtSource">源DataTable</param>   
  276.     /// <param name="strHeaderText">表头文本</param>   
  277.     /// <param name="strFileName">输出文件名,包含扩展名</param>   
  278.     /// <param name="oldColumnNames">要导出的DataTable列数组</param>   
  279.     /// <param name="newColumnNames">导出后的对应列名</param>   
  280.     public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName, string[] oldColumnNames, string[] newColumnNames)  
  281.     {  
  282.         ExportByWeb(dtSource, strHeaderText, strFileName, "sheet",oldColumnNames,newColumnNames);  
  283.     }  
  284.   
  285.     /// <summary>   
  286.     /// WEB导出DataTable到Excel   
  287.     /// </summary>   
  288.     /// <param name="dtSource">源DataTable</param>   
  289.     /// <param name="strHeaderText">表头文本</param>   
  290.     /// <param name="strFileName">输出文件名</param>   
  291.     /// <param name="strSheetName">工作表名称</param>   
  292.     public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName, string strSheetName)  
  293.     {  
  294.         HttpContext curContext = HttpContext.Current;  
  295.   
  296.         // 设置编码和附件格式      
  297.         curContext.Response.ContentType = "application/vnd.ms-excel";  
  298.         curContext.Response.ContentEncoding = Encoding.UTF8;  
  299.         curContext.Response.Charset = "";  
  300.         curContext.Response.AppendHeader("Content-Disposition",  
  301.             "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));  
  302.   
  303.         //生成列   
  304.         string columns = "";  
  305.         for (int i = 0; i < dtSource.Columns.Count; i++)  
  306.         {  
  307.             if (i > 0)  
  308.             {  
  309.                 columns += ",";  
  310.             }  
  311.             columns += dtSource.Columns[i].ColumnName;  
  312.         }  
  313.   
  314.         curContext.Response.BinaryWrite(Export(dtSource, strHeaderText,strSheetName,columns.Split(','),columns.Split(',')).GetBuffer());  
  315.         curContext.Response.End();  
  316.   
  317.     }  
  318.   
  319.     /// <summary>   
  320.     /// 导出DataTable到Excel   
  321.     /// </summary>   
  322.     /// <param name="dtSource">要导出的DataTable</param>   
  323.     /// <param name="strHeaderText">标题文字</param>   
  324.     /// <param name="strFileName">文件名,包含扩展名</param>   
  325.     /// <param name="strSheetName">工作表名</param>   
  326.     /// <param name="oldColumnNames">要导出的DataTable列数组</param>   
  327.     /// <param name="newColumnNames">导出后的对应列名</param>   
  328.     public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName, string strSheetName, string[] oldColumnNames, string[] newColumnNames)  
  329.     {  
  330.         HttpContext curContext = HttpContext.Current;  
  331.   
  332.         // 设置编码和附件格式      
  333.         curContext.Response.ContentType = "application/vnd.ms-excel";  
  334.         curContext.Response.ContentEncoding = Encoding.UTF8;  
  335.         curContext.Response.Charset = "";  
  336.         curContext.Response.AppendHeader("Content-Disposition",  
  337.             "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));  
  338.   
  339.         curContext.Response.BinaryWrite(Export(dtSource, strHeaderText, strSheetName,oldColumnNames,newColumnNames).GetBuffer());  
  340.         curContext.Response.End();  
  341.     }  
  342.   
  343.     /// <summary>读取excel      
  344.     /// 默认第一行为表头,导入第一个工作表   
  345.     /// </summary>      
  346.     /// <param name="strFileName">excel文档路径</param>      
  347.     /// <returns></returns>      
  348.     public static DataTable Import(string strFileName)  
  349.     {  
  350.         DataTable dt = new DataTable();  
  351.   
  352.         HSSFWorkbook hssfworkbook;  
  353.         using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))  
  354.         {  
  355.             hssfworkbook = new HSSFWorkbook(file);  
  356.         }  
  357.         ISheet sheet = hssfworkbook.GetSheetAt(0);  
  358.         System.Collections.IEnumerator rows = sheet.GetRowEnumerator();  
  359.   
  360.         IRow headerRow = sheet.GetRow(0);  
  361.         int cellCount = headerRow.LastCellNum;  
  362.   
  363.         for (int j = 0; j < cellCount; j++)  
  364.         {  
  365.             ICell cell = headerRow.GetCell(j);  
  366.             dt.Columns.Add(cell.ToString());  
  367.         }  
  368.   
  369.         for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)  
  370.         {  
  371.             IRow row = sheet.GetRow(i);  
  372.             DataRow dataRow = dt.NewRow();  
  373.   
  374.             for (int j = row.FirstCellNum; j < cellCount; j++)  
  375.             {  
  376.                 if (row.GetCell(j) != null)  
  377.                     dataRow[j] = row.GetCell(j).ToString();  
  378.             }  
  379.             dt.Rows.Add(dataRow);  
  380.         }  
  381.         return dt;  
  382.     }  
  383.     /// <summary>   
  384.     /// 从Excel中获取数据到DataTable   
  385.     /// </summary>   
  386.     /// <param name="strFileName">Excel文件全路径(服务器路径)</param>   
  387.     /// <param name="SheetName">要获取数据的工作表名称</param>   
  388.     /// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>   
  389.     /// <returns></returns>   
  390.     public static DataTable RenderDataTableFromExcel(string strFileName, string SheetName, int HeaderRowIndex)  
  391.     {  
  392.         using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))  
  393.         {  
  394.             IWorkbook workbook = new HSSFWorkbook(file);  
  395.             return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);  
  396.         }  
  397.     }  
  398.   
  399.     /// <summary>   
  400.     /// 从Excel中获取数据到DataTable   
  401.     /// </summary>   
  402.     /// <param name="strFileName">Excel文件全路径(服务器路径)</param>   
  403.     /// <param name="SheetIndex">要获取数据的工作表序号(从0开始)</param>   
  404.     /// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>   
  405.     /// <returns></returns>   
  406.     public static DataTable RenderDataTableFromExcel(string strFileName, int SheetIndex, int HeaderRowIndex)  
  407.     {  
  408.         using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))  
  409.         {  
  410.             IWorkbook workbook = new HSSFWorkbook(file);  
  411.             string SheetName = workbook.GetSheetName(SheetIndex);  
  412.             return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);  
  413.         }  
  414.     }  
  415.   
  416.     /// <summary>   
  417.     /// 从Excel中获取数据到DataTable   
  418.     /// </summary>   
  419.     /// <param name="ExcelFileStream">Excel文件流</param>   
  420.     /// <param name="SheetName">要获取数据的工作表名称</param>   
  421.     /// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>   
  422.     /// <returns></returns>   
  423.     public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, string SheetName, int HeaderRowIndex)  
  424.     {  
  425.         IWorkbook workbook = new HSSFWorkbook(ExcelFileStream);  
  426.         ExcelFileStream.Close();  
  427.         return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);  
  428.     }  
  429.   
  430.     /// <summary>   
  431.     /// 从Excel中获取数据到DataTable   
  432.     /// </summary>   
  433.     /// <param name="ExcelFileStream">Excel文件流</param>   
  434.     /// <param name="SheetIndex">要获取数据的工作表序号(从0开始)</param>   
  435.     /// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>   
  436.     /// <returns></returns>   
  437.     public static DataTable RenderDataTableFromExcel(Stream ExcelFileStream, int SheetIndex, int HeaderRowIndex)  
  438.     {  
  439.         IWorkbook workbook = new HSSFWorkbook(ExcelFileStream);  
  440.         ExcelFileStream.Close();  
  441.         string SheetName = workbook.GetSheetName(SheetIndex);  
  442.         return RenderDataTableFromExcel(workbook, SheetName, HeaderRowIndex);  
  443.     }  
  444.   
  445.     /// <summary>   
  446.     /// 从Excel中获取数据到DataTable   
  447.     /// </summary>   
  448.     /// <param name="workbook">要处理的工作薄</param>   
  449.     /// <param name="SheetName">要获取数据的工作表名称</param>   
  450.     /// <param name="HeaderRowIndex">工作表标题行所在行号(从0开始)</param>   
  451.     /// <returns></returns>   
  452.     public static DataTable RenderDataTableFromExcel(IWorkbook workbook, string SheetName, int HeaderRowIndex)  
  453.     {  
  454.         ISheet sheet = workbook.GetSheet(SheetName);  
  455.         DataTable table = new DataTable();  
  456.         try  
  457.         {  
  458.             IRow headerRow = sheet.GetRow(HeaderRowIndex);  
  459.             int cellCount = headerRow.LastCellNum;  
  460.   
  461.             for (int i = headerRow.FirstCellNum; i < cellCount; i++)  
  462.             {  
  463.                 DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);  
  464.                 table.Columns.Add(column);  
  465.             }  
  466.   
  467.             int rowCount = sheet.LastRowNum;  
  468.  
  469.             #region 循环各行各列,写入数据到DataTable   
  470.             for (int i = (sheet.FirstRowNum + 1); i < sheet.LastRowNum; i++)  
  471.             {  
  472.                 IRow row = sheet.GetRow(i);  
  473.                 DataRow dataRow = table.NewRow();  
  474.                 for (int j = row.FirstCellNum; j < cellCount; j++)  
  475.                 {  
  476.                     ICell cell = row.GetCell(j);  
  477.                     if (cell == null)  
  478.                     {  
  479.                         dataRow[j] = null;  
  480.                     }  
  481.                     else  
  482.                     {  
  483.                         //dataRow[j] = cell.ToString();   
  484.                         switch (cell.CellType)  
  485.                         {  
  486.                             case CellType.BLANK:  
  487.                                 dataRow[j] = null;  
  488.                                 break;  
  489.                             case CellType.BOOLEAN:  
  490.                                 dataRow[j] = cell.BooleanCellValue;  
  491.                                 break;  
  492.                             case CellType.NUMERIC:  
  493.                                 dataRow[j] = cell.ToString();  
  494.                                 break;  
  495.                             case CellType.STRING:  
  496.                                 dataRow[j] = cell.StringCellValue;  
  497.                                 break;  
  498.                             case CellType.ERROR:  
  499.                                 dataRow[j] = cell.ErrorCellValue;  
  500.                                 break;  
  501.                             case CellType.FORMULA:  
  502.                             default:  
  503.                                 dataRow[j] = "=" + cell.CellFormula;  
  504.                                 break;  
  505.                         }  
  506.                     }  
  507.                 }  
  508.                 table.Rows.Add(dataRow);  
  509.                 //dataRow[j] = row.GetCell(j).ToString();   
  510.             }  
  511.             #endregion   
  512.         }  
  513.         catch (System.Exception ex)  
  514.         {  
  515.             table.Clear();  
  516.             table.Columns.Clear();  
  517.             table.Columns.Add("出错了");  
  518.             DataRow dr = table.NewRow();  
  519.             dr[0] = ex.Message;  
  520.             table.Rows.Add(dr);  
  521.             return table;  
  522.         }  
  523.         finally  
  524.         {  
  525.             //sheet.Dispose();   
  526.             workbook = null;  
  527.             sheet = null;  
  528.         }  
  529.         #region 清除最后的空行   
  530.         for (int i = table.Rows.Count - 1; i > 0; i--)  
  531.         {  
  532.             bool isnull = true;  
  533.             for (int j = 0; j < table.Columns.Count; j++)  
  534.             {  
  535.                 if (table.Rows[i][j] != null)  
  536.                 {  
  537.                     if (table.Rows[i][j].ToString() != "")  
  538.                     {  
  539.                         isnull = false;  
  540.                         break;  
  541.                     }  
  542.                 }  
  543.             }  
  544.             if (isnull)  
  545.             {  
  546.                 table.Rows[i].Delete();  
  547.             }  
  548.         }  
  549.         #endregion   
  550.         return table;  
  551.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值