posts - 2,  comments - 33,  trackbacks - 0
下载地址:
http://go.microsoft.com/?linkid=6899996

帮助文档里有一个创建docx的例子,下面提供一个创建xlsx的例子

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.DocumentFormat.OpenXml.Packaging;
using System.IO;
using System.Xml;


namespace ConsoleApplication1
{
    
class Program
    
{
        
static void Main(string[] args)
        
{           
            CreateNewXlsDocument(
"c:\\temp.xlsx");
        }

        

        
public static void CreateNewXlsDocument(string document)
        
{         
            
using (SpreadsheetDocument doc = SpreadsheetDocument.Create(document, SpreadsheetDocumentType.Workbook))
            
{              

                WorkbookPart mainPart 
= doc.AddWorkbookPart();
                WorksheetPart part 
= mainPart.AddNewPart<WorksheetPart>();

                
string rid = mainPart.GetIdOfPart(part);

                SetWorkBookContect(mainPart,rid);
                SetWorkSheetContect(part);
            }

        }


        
public static void SetWorkBookContect(WorkbookPart part,string rid)
        
{
            
const string xlsXml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?><workbook xmlns=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships""><sheets><sheet name=""Sheet1"" sheetId=""1"" r:id=""{0}""/></sheets></workbook>";
            
using (Stream stream = part.GetStream())
            
{
                
byte[] buf = (new UTF8Encoding()).GetBytes(string.Format(xlsXml,rid));
                stream.Write(buf, 
0, buf.Length);
            }

        }



        
public static void SetWorkSheetContect(WorksheetPart part)
        
{
            
const string xlsXml =
         
@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<worksheet xmlns=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"">
  <sheetData>
    <row>
      <c t=""inlineStr"">
        <is>
          <t>测试</t>
        </is>
      </c>      
    </row>    
  </sheetData>
</worksheet>
";
                       
            
using (Stream stream = part.GetStream())
            
{
                
byte[] buf = (new UTF8Encoding()).GetBytes(xlsXml);
                stream.Write(buf, 
0, buf.Length);
            }

        }


    }

}

posted on 2007-06-06 17:03 德里克 阅读(2167) 评论(4)  编辑 收藏 所属分类: Office 2007

FeedBack:
2007-06-07 09:20 | 双鱼座      
还是CTP版本,不敢在项目中使用
  回复  引用  查看    
2007-06-07 23:05 | 乐趣电子书 [未注册用户]
还是CTP版本,不敢在项目中使用
  回复  引用    
2007-07-15 19:56 | 没有耳多 [未注册用户]
功能还是相当的弱.

我现在也在用它....


其本上,只能做做像这样的示例.
  回复  引用    
2007-07-17 14:28 | coffeecherry [未注册用户]
这个例子可以生成workbook.xml和 sheet1.xml,但是没有sharedStrings.xml,不太规范啊,呵呵。。。
  回复  引用    

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-06-07 16:01 编辑过
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接:
 




<2007年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

与我联系

搜索

 

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

相册

Links

最新评论

阅读排行榜

评论排行榜