I am currently writing an application (C#) to generate reports in a excel file, based on other reports.
我目前正在编写一个应用程序(C#),根据其他报告在Excel文件中生成报告。
The problem is that, once get an number from a certain sheet, and copy to another one, the destination cell is not formated correctly, and the number does no display correctly.
问题是,一旦从某个工作表中获取一个数字,并复制到另一个工作表,目标单元格没有正确格式化,并且该数字无法正确显示。
E.g :
Number from source : 14.34
Number on destination : 14.345661
How do i format the destination cell to force the number formating to be the same as the source cell.
如何格式化目标单元格以强制数字格式与源单元格相同。
Thanks !
谢谢 !
2 个解决方案
#1
4
The format of a given cell/range in excel can be set via code.
excel中给定单元格/范围的格式可以通过代码设置。
public void SetCustomFormat(string format, int r, int c)
{
((Excel.Range)xlWks.Cells[r, c]).NumberFormat = format;
}
In your specific case, I believe you would need to use the format "#.00" or "#.##"
在您的具体情况下,我认为您需要使用“#.00”或“#。##”格式
#2
3
Here's a snippet from some code I've got in use, to show you the general pattern for formatting cells. Obviously, there are some variables declared, but it should show you what you need.
这是我正在使用的一些代码的片段,向您展示格式化单元格的一般模式。显然,有一些变量被声明,但它应该显示你需要的东西。
sheet.get_Range("A" + CurrentRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).Font.Bold = true;
sheet.get_Range("A" + CurrentRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).Interior.Color = Color.Silver.ToArgb();
sheet.get_Range("A" + CurrentRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
sheet.get_Range("A" + CompetencyStartRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
That first line, assuming CurrentRowIndex = 1 and ColPrefix = "B", replacing the variables with the resulting values would translate into
即第一行中,假设CurrentRowIndex = 1和ColPrefix =“B”,用所得的值将转化为替换变量
sheet.get_Range("A1", "B1").Font.Bold = true;
At any rate, you want to set the numberformat. (Coming..)
无论如何,您要设置数字格式。 (未来..)
sheet.Cells[Row, Column].NumberFormat = "0.00"
#1
4
The format of a given cell/range in excel can be set via code.
excel中给定单元格/范围的格式可以通过代码设置。
public void SetCustomFormat(string format, int r, int c)
{
((Excel.Range)xlWks.Cells[r, c]).NumberFormat = format;
}
In your specific case, I believe you would need to use the format "#.00" or "#.##"
在您的具体情况下,我认为您需要使用“#.00”或“#。##”格式
#2
3
Here's a snippet from some code I've got in use, to show you the general pattern for formatting cells. Obviously, there are some variables declared, but it should show you what you need.
这是我正在使用的一些代码的片段,向您展示格式化单元格的一般模式。显然,有一些变量被声明,但它应该显示你需要的东西。
sheet.get_Range("A" + CurrentRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).Font.Bold = true;
sheet.get_Range("A" + CurrentRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).Interior.Color = Color.Silver.ToArgb();
sheet.get_Range("A" + CurrentRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
sheet.get_Range("A" + CompetencyStartRowIndex.ToString(), ColPrefix + CurrentRowIndex.ToString()).BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
That first line, assuming CurrentRowIndex = 1 and ColPrefix = "B", replacing the variables with the resulting values would translate into
即第一行中,假设CurrentRowIndex = 1和ColPrefix =“B”,用所得的值将转化为替换变量
sheet.get_Range("A1", "B1").Font.Bold = true;
At any rate, you want to set the numberformat. (Coming..)
无论如何,您要设置数字格式。 (未来..)
sheet.Cells[Row, Column].NumberFormat = "0.00"