Excel读取与创建方法三: MyXLS

在后台调用excel组件,生成Excel,虽然可以Excel文件进行完全控制,可以生成任何复杂的格式,但是有个很大的缺点,这种方式会产生很多Excel进程很难完全清掉,特别是在出错的时候,可能会使整个服务器崩溃。本文为大家介绍一个C#写的开源组件,并简单说下office2003和以上版本支持的XML格式。

 操作Excel二进制格式

    OpenOffice.org发布过的俩个文档Excel File Format (BIFF8)SpecificationMicrosoft CompoundDocument (OLE2) Format SpecificationExcel的二进制格式做了一个比较详细的说明,依靠这些信息,我们可以直接操作Office二进制格式文档。

  MyXls是一个C#写的开源组件,可以用来生成具有很多表格且包含格式的Excel文件。它提供了一套基于对象的API,非常容易使用。

1,生成一个空的表格
       

3, 再创建一个复杂点表格

   protected void Page_Load(object sender, EventArgs e)
        {
            org.in2bits.MyXls.XlsDocument doc = new XlsDocument();
            doc.FileName = "TestingAgain.xls";

            //doc.Workbook.ProtectContents = true;

            for (int s = 1; s <= 5; s++)
            {
                string sheetName = Request.Form["txtSheet" + s].Replace(",", string.Empty);
               
                if (sheetName.Trim() == string.Empty)
                    continue;

                int rowMin, rowCount, colMin, colCount;

                try
                {
                    rowMin = int.Parse(Request.Form["txtRowMin" + s]);
                    rowCount = int.Parse(Request.Form["txtRows" + s]);
                    colMin = int.Parse(Request.Form["txtColMin" + s]);
                    colCount = int.Parse(Request.Form["txtCols" + s]);
                }
                catch
                {
                    continue;
                }

                if (rowCount > 65535) rowCount = 65535;
                if (rowCount < 0) rowCount = 0;
                if (rowMin < 1) rowMin = 1;
                if (rowMin > 32767) rowMin = 32767;

                if (colCount > 255) colCount = 255;
                if (colCount < 1) colCount = 1;
                if (colMin < 1) colMin = 1;
                if (colMin > 100) colMin = 100;

                if (sheetName.Length > 35) sheetName = sheetName.Substring(0, 35);

                Worksheet sheet = doc.Workbook.Worksheets.Add(sheetName);
                //sheet.Protected = true;
                Cells cells = sheet.Cells;

                for (int row = 0; row <= rowCount; row++)
                {
                    if (row == 0)
                    {
                        for (int col = 1; col <= colCount; col++)
                        {
                            Cell cell = cells.Add(rowMin + row, colMin + col - 1, "Fld" + col);
                            cell.Font.Weight = 700;
                            cell.DiagonalAscending = true;
                            cell.DiagonalLineColor = Colors.Black;
                            cell.DiagonalLineStyle = 2;
                            //cell.Pattern = 18;
                            //cell.PatternColorIndex = 0;
                            //cell.PatternBackgroundColor = Colors.Green;
                            cell.Locked = true;
                        }
                    }
                    else
                    {
                        for (int col = 1; col <= colCount; col++)
                        {
                            Cell cell = cells.Add(rowMin + row, colMin + col - 1, /*row + col*/1.001);
                            cell.Locked = false;
                        }
                    }
                }
            }

            doc.Send();
            Response.Flush();
            Response.End();
        }

1 XlsDocument xls = new XlsDocument(); //创建一个空的Excel文档
2        
3 xls.Send(); //将文档发送到浏览器。


2, 创建一个复杂点表格

 XlsDocument xls = new XlsDocument();
            xls.FileName 
= "Wacky.xls";

            
//添加文件属性
            xls.SummaryInformation.Author = "Tim Erickson"//作者
            xls.SummaryInformation.Subject = "A wacky display of Excel file generation";
            xls.DocumentSummaryInformation.Company 
= "in2bits.org";

            
for (int sheetNumber = 1; sheetNumber <= 5; sheetNumber++)
            
{
                
string sheetName = "Sheet " + sheetNumber;
                
int rowMin = sheetNumber;
                
int rowCount = sheetNumber + 10;
                
int colMin = sheetNumber;
                
int colCount = sheetNumber + 10;
                
//创建5个表格
                Worksheet sheet = xls.Workbook.Worksheets.AddNamed(sheetName);
                Cells cells 
= sheet.Cells;
                
for (int r = 0; r < rowCount; r++)
                
{
                    
if (r == 0)
                    
{
                        
for (int c = 0; c < colCount; c++)
                        
{
                            
//在一行内创建colCount个单元格
                            cells.Add(rowMin + r, colMin + c, "Fld" + (c + 1)).Font.Bold = true;
                        }

                    }

                    
else
                    
{
                        
for (int c = 0; c < colCount; c++)
                        
{
                            
int val = r + c;
                            Cell cell 
= cells.Add(rowMin + r, colMin + c, val);
                            
if (val % 2 != 0)
                            
{
                                cell.Font.FontName 
= "Times New Roman";
                                cell.Font.Underline 
= UnderlineTypes.Double;//给文字下方加一个双下划线
                                cell.Rotation = 45;//单元格文字旋转45度
                            }

                        }

                    }

                }

            }
posted @ 2009-05-28 22:19  Cad人生  阅读(967)  评论(0编辑  收藏  举报