如果有多个excel文件,则使用c#在excel中设置边框属性无效
我正在使用c#中的Microsoft.Office.Interop.Excel库设计一个小程序。我的程序读取excel文件(其名称为1.xlss、2.xlsx等)并创建一个新的excel文件,然后将所有excel文件的数据复制到新的excel文件中,并留有一行间距。我还设置了某些属性,如单元格的边框、字体、背景色等 对于第一个excel文件(1.xls),一切都很好,但是对于第二个、第三个等等,它并没有设置单元格的border属性如果有多个excel文件,则使用c#在excel中设置边框属性无效,c#,C#,我正在使用c#中的Microsoft.Office.Interop.Excel库设计一个小程序。我的程序读取excel文件(其名称为1.xlss、2.xlsx等)并创建一个新的excel文件,然后将所有excel文件的数据复制到新的excel文件中,并留有一行间距。我还设置了某些属性,如单元格的边框、字体、背景色等 对于第一个excel文件(1.xls),一切都很好,但是对于第二个、第三个等等,它并没有设置单元格的border属性 // data member initialization fo
// data member initialization for reading the sheet
Excel.Application app;
Excel.Workbook workbook;
Excel.Worksheet worksheet;
Excel.Range range;
// data member initialization for writing sheet
Excel.Application finalApp;
Excel.Workbook finalWorkBook;
Excel.Worksheet finalWorkSheet;
String path = this.textBox1.Text;
String numberOfFiles = (String)this.comboBox1.SelectedItem;
int count = 1;
int row = 1, col = 1;
try
{
// Object creation for the final sheet
finalApp = new Excel.ApplicationClass();
finalApp.Visible = true;
finalWorkBook = finalApp.Workbooks.Add(1);
finalWorkSheet = (Excel.Worksheet)finalWorkBook.Sheets[1];
// opening a excel file
app = new Excel.ApplicationClass();
Excel.Borders b=null;
Excel.Borders fb = null;
try
{
for (int k = 0; k < Int32.Parse(numberOfFiles); k++)
{
fullPath = @path + @"\" + count + ".xlsx";
workbook = app.Workbooks.Open(fullPath, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value);
worksheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
range = worksheet.UsedRange;
int cnum1 = range.Columns.Count;
int rnum1 = range.Rows.Count;
int i, j;
for (i = 1; i <= rnum1; i++)
{
for (j = 1; j <= cnum1; j++)
{
if ((range.Cells[i, j] as Excel.Range).Value2 != null)
{
string value1 = (range.Cells[i, j] as Excel.Range).Value2.ToString();
finalWorkSheet.Cells[row, col] = value1;
b= (Excel.Borders)(range.Cells[i, j] as Excel.Range).Borders;
//MessageBox.Show(b.Weight.ToString());
fb=(finalWorkSheet.Cells[i, j] as Excel.Range).Borders;
fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight= b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight;
fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight;
fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight;
fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight;
fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle;
fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].LineStyle = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].LineStyle;
fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle;
fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle;
//fb.Color = b.Color;
//(finalWorkSheet.Cells[i, j] as Excel.Range).BorderAround(b.LineStyle,(Excel.XlBorderWeight)b.Weight,
//(Excel.XlColorIndex)b.ColorIndex,b.Color);
}
(finalWorkSheet.Cells[row, col] as Excel.Range).Interior.Color = (range.Cells[i, j] as Excel.Range).Interior.Color;
(finalWorkSheet.Cells[row, col] as Excel.Range).Font.Color = (range.Cells[i, j] as Excel.Range).Font.Color;
col++;
}
row++;
col = 1;
}
//finalWorkBook.SaveAs("hello.xlsx", Excel.XlFileFormat.xlExcel4Workbook, Missing.Value,
//Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value,
//Missing.Value, Missing.Value, Missing.Value);
workbook.Close(false, false, Missing.Value);
count++;
row++;
col = 1;
}
}
catch (FileNotFoundException fnfe)
{
MessageBox.Show("Error while opening the file "+fullPath);
}
//finalWorkBook.Close(true,false,Missing.Value);
}
catch (Exception ex)
{
MessageBox.Show(@"Some Error has occurred.Please check the path Correctly
whether it's correct or not");
}
}
//读取工作表的数据成员初始化
Excel应用程序;
Excel.工作簿;
Excel.工作表;
范围;
//写入工作表的数据成员初始化
Excel.applicationfinalapp;
Excel。工作簿最终工作手册;
Excel。工作表最终工作表;
字符串路径=this.textBox1.Text;
String numberOfFiles=(String)this.comboBox1.SelectedItem;
整数计数=1;
int行=1,列=1;
尝试
{
//为最终图纸创建对象
finalApp=new Excel.ApplicationClass();
finalApp.Visible=true;
finalWorkBook=finalApp.Workbooks.Add(1);
finalWorkSheet=(Excel.Worksheet)finalWorkBook.Sheets[1];
//打开excel文件
app=新建Excel.ApplicationClass();
Excel.Borders b=null;
Excel.Borders fb=null;
尝试
{
for(intk=0;k
fb=(finalWorkSheet.Cells[row, col] as Excel.Range).Borders;
fb=(finalWorkSheet.Cells[row, col] as Excel.Range).Borders;