Winform技巧:TreeView导入Excel

Winform技巧:TreeView导入Excel

大家好,又见面了,我是全栈君。

Winform技巧:TreeView导入Excel
将如上效果导入到Excel中,效果如:
Winform技巧:TreeView导入Excel
原理:先将数据按照你想要的表格效果做成DataTable,然后在导入到Excel中
做成DataTable的方法:
Code
第53行为导入到Excel的公共类,SaveAS是导入的具体方法
Code

InBlock.gif 1 
using System; 


InBlock.gif 2 
using System.Collections.Generic; 


InBlock.gif 3 
using System.ComponentModel; 


InBlock.gif 4 
using System.Data; 


InBlock.gif 5 
using System.Drawing; 


InBlock.gif 6 
using System.Text; 


InBlock.gif 7 
using System.Windows.Forms; 


InBlock.gif 8 
using System.IO; 


InBlock.gif 9    


InBlock.gif10 
namespace 教务管理系统 


InBlock.gif11 { 


InBlock.gif12         
class DGVToExcel 


InBlock.gif13         { 


InBlock.gif14                 
public 
void SaveAs(DataTable dt) 
//另存新档按钮     导出成Excel 


InBlock.gif15                 { 


InBlock.gif16                         
try 


InBlock.gif17                         { 


InBlock.gif18                                 SaveFileDialog saveFileDialog = 
new SaveFileDialog(); 


InBlock.gif19                                 saveFileDialog.Filter = 
“Execl files (*.xls)|*.xls”


InBlock.gif20                                 saveFileDialog.FilterIndex = 0; 


InBlock.gif21                                 saveFileDialog.RestoreDirectory = 
true


InBlock.gif22                                 saveFileDialog.CreatePrompt = 
true


InBlock.gif23                                 saveFileDialog.Title = 
“导出Excel到”


InBlock.gif24                                 
if(saveFileDialog.ShowDialog()==DialogResult.OK) 


InBlock.gif25                                 { 


InBlock.gif26                                 Stream myStream; 


InBlock.gif27                                 myStream = saveFileDialog.OpenFile(); 


InBlock.gif28                                 StreamWriter sw = 
new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); 


InBlock.gif29                                 
string str = “”; 


InBlock.gif30                                 
try 


InBlock.gif31                                 { 


InBlock.gif32                                         
//写标题 


InBlock.gif33                                        


InBlock.gif34                                                 str +=dt.TableName; 


InBlock.gif35                                         sw.WriteLine(str); 


InBlock.gif36                                         
//写内容 


InBlock.gif37                                         
for (
int j = 0; j < dt.Rows.Count; j++) 


InBlock.gif38                                         { 


InBlock.gif39                                                 
string tempStr = “”; 


InBlock.gif40                                                 
for (
int k = 0; k < dt.Columns.Count; k++) 


InBlock.gif41                                                 { 


InBlock.gif42                                                         
if (k > 0) 


InBlock.gif43                                                         { 


InBlock.gif44                                                                 tempStr += 
“\t”


InBlock.gif45                                                         } 


InBlock.gif46                                                         tempStr += dt.Rows[j][k].ToString(); 


InBlock.gif47                                                 } 


InBlock.gif48                                                 sw.WriteLine(tempStr); 


InBlock.gif49                                         } 


InBlock.gif50                                         sw.Close(); 


InBlock.gif51                                         myStream.Close(); 


InBlock.gif52                                 } 


InBlock.gif53                                 
catch 


InBlock.gif54                                 { 


InBlock.gif55    


InBlock.gif56                                 } 


InBlock.gif57                                 
finally 


InBlock.gif58                                 { 


InBlock.gif59                                         sw.Close(); 


InBlock.gif60                                         myStream.Close(); 


InBlock.gif61                                 } 


InBlock.gif62                                 } 


InBlock.gif63                         } 


InBlock.gif64                         
catch 


InBlock.gif65                         { } 


InBlock.gif66                 }            


InBlock.gif67    


InBlock.gif68         } 


InBlock.gif69 } 


InBlock.gif70

此功能的实现需要一个.dll文件,文件全称为:Interop.Excel.dll,加载到你的项目中即可。
本文转自叶子文文博客51CTO博客,原文链接http://blog.51cto.com/leafwf/185798如需转载请自行联系原作者

叶子文文
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/108014.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号