惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
Jina AI
Jina AI
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
I
InfoQ
L
LangChain Blog
The Cloudflare Blog
IT之家
IT之家
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
博客园 - 聂微东
美团技术团队
博客园_首页

博客园 - neverlost

客户端开发杂记 html5 元素 来自 nativeformelements.com 闲扯,面向对象的ext4中的一些事儿1 Ext 4 beta1 发布似乎仍不给力 浅述教学上基于Media Player的视频切片方式 webservice传输数据量较大的情况的解决方案 - neverlost - 博客园 现场保障系统开发过程中增加并行处理(一) vs2010中文版+codesmith 5.2 安装失败 转的 winform开发连接webservice中单向证书 .net下开发windows服务的经验 .net 写的 webservice 给java调用 ext中使用tab方式 ext做列表页面关于查询多行的办法 关于架构的问题 ext的grid 获取页面内容方式 - neverlost - 博客园 微软.net下 charting 要注意的事情 - neverlost .net 获取 其他类型的webservice的方式以及看法 2条路 代码生成 or 配置 2.1 2条路 代码生成 or 配置 2
.net 下比较蛋疼的word 表格转excel表格
neverlost · 2011-01-04 · via 博客园 - neverlost

应朋友要求,帮忙做个小工具就方便他从word中的表格里把数据按照他们的要求转到excel表格,做了之后发现挺蛋疼的,估计是水平问题。当然主要原因还是见识不够,嘿嘿。

对于这个小程序反正做也做了 正好做个记录以后说不定用的到。

//这个部分是打开word文件 其实这里没啥网上资料一大把找找汇总下就成。
object oReadOnly = true;
object oMissing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word._Application oWord 

= null;
Microsoft.Office.Interop.Word._Document oDoc;
oWord 
= new Microsoft.Office.Interop.Word.Application();
oWord.Visible 
= true;//只是为了方便观察
oDoc = oWord.Documents.Open(ref docfilename, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
    
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

Microsoft.Office.Interop.Excel.ApplicationClass oExcel;
oExcel 

= new Microsoft.Office.Interop.Excel.ApplicationClass();
oExcel.UserControl 
= false;
Microsoft.Office.Interop.Excel.WorkbookClass wb 
= (Microsoft.Office.Interop.Excel.WorkbookClass)oExcel.Workbooks.Add(System.Reflection.Missing.Value);
//新建一个Sheet,新建后会默认处于焦点状态
wb.Worksheets.Add(oMissing, oMissing, 1, oMissing);
//然后开始循环读word的内容了 
//第一次自己用的是for循环结果那时间太给力了,等的我花都谢了,还是foreach快,这是有道理地。
foreach (Paragraph item in oDoc.Paragraphs)
{
    
//下面都是自己的逻辑了没啥特别的就是 把word内容暂时读取出来丢一个list对象中。
    if (item.Range.Text != "\r\a")
    {
    
///。。。。都自己的逻辑
    } 
}
//这里开始折腾excel了就开始蛋疼了 
int rownum = 1;
for (int i = 0; i < ilist.Count; i++//ilist是列信息
{
    
if (ilist[i].DisplayName.Length == 0
    {
        oExcel.Cells[rownum, i 
+ 1= ilist[i].Mapname; //excel必须从第一行第一列开始填充数据。
    }
    
else
    {
        oExcel.Cells[rownum, i 
+ 1= ilist[i].DisplayName;
         
    }
}
/// 然后经过类似上面的excel每个单元格的数据填充的过程后 终于来到结束部分

//保存工作簿
wb.Saved = true;
object efileName =(Object)docfilename.ToString().Replace("doc""xls");
//生成文件,释放资源
//oExcel.ActiveWorkbook.SaveCopyAs(efileName + "");
oExcel.DisplayAlerts = false;
oExcel.ActiveWorkbook.SaveAs(efileName,XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);

oExcel.Quit();

 
oDoc.Close();
oWord.Quit(); 

///有没感觉怪?是的 word退出了,那个正常 excel反正没打开过,但是在进程里一看,居然有个excel在郁闷啊,查了下貌似都关不掉,只有用杀进程的办法可以直接关。当然不是指的在任务管理器里杀而是在程序里。