自动处理Excel文档,删除空格,删除空白行(列),标红操作
CellRange ranges = sheet.Range[p "4"];
string strValue = ranges.Value2.ToString();
if (strValue.Contains('~'))//筛选,如果获取的字符串里有~就执行下面的循环
{
strValue= strValue.Replace(")","");
strValue = strValue.Replace("(", "");
strValue = strValue.Replace(" ", "");//此处用了Replace方法,用括号后的字段代替前面的
string[] sArray = strValue.Split('~');
int X = sheet.Rows.Count();
CellRange rangeX = sheet.Range[p "5:" p X];//获取数据范围
ConditionalFormatWrapper formatx = rangeX.ConditionalFormats.AddCondition();//在所选范围里定义条件格式x
formatx.FormatType = ConditionalFormatType.CellValue;//条件格式的类型为各自值设置单元格的类型
formatx.FirstFormula = sArray[0];
formatx.SecondFormula = sArray[1];
formatx.Operator = ComparisonOperatorType.NotBetween;
formatx.IsBold = true;
formatx.BackColor = Color.Red;
//在集合sArray里的数据位界限
}
else
{
strValue = strValue.Replace("(","");
strValue = strValue.Replace(")", "");
strValue = strValue.Replace("-", "");
if (strValue.Contains('A'))
{
int X = sheet.Rows.Count();
CellRange rangeY = sheet.Range[p "5:" p X];
ConditionalFormatWrapper formatx = rangeY.ConditionalFormats.AddCondition();
formatx.FormatType = ConditionalFormatType.CellValue;
formatx.FirstFormula = "0";
formatx.Operator = ComparisonOperatorType.NotEqual;
formatx.IsBold = true;
}
else
{
if (strValue.Length ==0)
{
int X = sheet.Rows.Count();
CellRange rangeZ = sheet.Range[p "5:" p X];
ConditionalFormatWrapper formatx = rangeZ.ConditionalFormats.AddCondition();
formatx.FormatType = ConditionalFormatType.CellValue;
formatx.FirstFormula = "0";
formatx.Operator = ComparisonOperatorType.NotEqual;
formatx.IsBold = true;
}
else
{
if (strValue.Length <= 15)
{
int X = sheet.Rows.Count();
CellRange rangeZ = sheet.Range[p "5:" p X];
ConditionalFormatWrapper formatx = rangeZ.ConditionalFormats.AddCondition();
formatx.FormatType = ConditionalFormatType.CellValue;
formatx.FirstFormula = strValue;
formatx.Operator = ComparisonOperatorType.NotEqual;
formatx.IsBold = true;
formatx.BackColor = Color.Red;
}
else
{
int X = sheet.Rows.Count();
CellRange rangeZ = sheet.Range[p "5:" p X];
ConditionalFormatWrapper formatx = rangeZ.ConditionalFormats.AddCondition();
formatx.FormatType = ConditionalFormatType.CellValue;
formatx.FirstFormula = "0";
formatx.Operator = ComparisonOperatorType.Equal;
formatx.IsBold = true;
formatx.BackColor = Color.Red;
}
}
}
}
}
string filename = Path.GetFileName(ExcelFilePath);
try { workbook.SaveToFile(@"F:\" filename);
System.Diagnostics.Process.Start(@"F:\" filename);
}
catch(Exception)
{
MessageBox.Show("你要保存的路径已被另一个同名文件占用!");
MessageBox.Show("此文件将被保存在D盘根目录");
workbook.SaveToFile(@"D:\" filename);
System.Diagnostics.Process.Start(@"D:\" filename);
}
}
}
}
评论