Get data style from Excel

weixin_34377065發表於2012-07-16
try
            {
                Excel.Application app = new Excel.Application();
                string excelFilePath = @"H:\22.xlsx";
                Excel.Workbook book = app.Workbooks.Open(excelFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                StringBuilder sb = new StringBuilder();
                Excel.Worksheet sheet = book.Sheets[1] as Worksheet;
                for (int i = 0; i < 4; i++)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        try
                        {
                            Excel.Range range = (Excel.Range)sheet.Cells[i, k];
                            string fontName = range.Font.Name as string;
                            string fontSize = ((int)range.Font.Size).ToString();

                            string bgColor = (range.Interior.Color as object).ToString();
                            string height = ((float)range.Height).ToString();
                            string width = ((float)range.Width).ToString();

                            
                            string valign = GetVerticalAlignmentName((int)range.VerticalAlignment);
                            string halign = GetHorizontalAlignmentName((int)range.HorizontalAlignment);

                            string text = (range.Text as object).ToString();

                            string interior = range.Interior.Color as string;
                            
                            sb.AppendLine("Row:" + i.ToString() + " Col:" + k.ToString() + " Font:" + fontName);
                            sb.AppendLine("Row:" + i.ToString() + " Col:" + k.ToString() + " fontSize:" + fontSize);

                            sb.AppendLine("Row:" + i.ToString() + " Col:" + k.ToString() + " bgColor:" + bgColor);
                            sb.AppendLine("Row:" + i.ToString() + " Col:" + k.ToString() + " height:" + height);
                            sb.AppendLine("Row:" + i.ToString() + " Col:" + k.ToString() + " width:" + width);
                            sb.AppendLine("Row:" + i.ToString() + " Col:" + k.ToString() + " text:" + text);

                            sb.AppendLine("Row:" + i.ToString() + " Col:" + k.ToString() + " valign:" + valign);

                            sb.AppendLine("Row:" + i.ToString() + " Col:" + k.ToString() + " interior:" + interior);


                        }
                        catch(Exception ex)
                        {
                            //sb.AppendLine(ex.ToString());
 
                        }
                    }
                }
                this.richTextBox1.Text = sb.ToString();

                book.Close(true, Type.Missing, Type.Missing);
                book = null;
                app.Quit();
                app = null;
                
            }
            catch
            {
 
            }

 

相關文章