java excel导出 弹出对话框_导出Excel 时,如何弹出窗体并选择保存地址

—-

你可以先弹出一个SaveFileDialog,选择地址之后再作为参数传给,你导出Excel的方法,导出Excel

—-

string name = DNTRequest.GetString(“fname”);

Aspose.Cells.Workbook workbook = new Workbook();

workbook.Worksheets[0].Cells[0, 0].Value = name;

SaveFileDialog sfd = new SaveFileDialog();

if (sfd.ShowDialog() == DialogResult.OK)

{

workbook.Save(sfd.FileName);

}

filename是获取选择的地址吗,

这样报错啊

—-

线程错误。。

在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常。

—- 20分

a href=”http://www.cnblogs.com/Holmes-Jin/archive/2012/08/22/2650387.html” target=”_blank”>http://www.cnblogs.com/Holmes-Jin/archive/2012/08/22/2650387.html

—-

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

{

string file = saveFileDialog1.FileName;

。。。

}

—-

这里有个例子。。。http://download.csdn.net/detail/superior_yong/8040249

—-

导出Excel 文档,选择保存地址到Word文档示例

//选择word文档保存地址

private void btn_SelectTxt_Click(object sender, EventArgs e)

{

FolderBrowserDialog FBDialog=new FolderBrowserDialog();//实例化浏览文件夹对话框对象

if (FBDialog.ShowDialog() == DialogResult.OK)//判断是否选择了文件夹

{

txt_Word.Text = FBDialog.SelectedPath;//显示选择的Word存放路径

}

}

//导出Excel文档

private void btn_Read_Click(object sender, EventArgs e)

{

object missing = System.Reflection.Missing.Value;//获取缺少的object类型值

string[] P_str_Names = txt_Excel.Text.Split(‘,’);//存储所有选择的Excel文件名

object P_obj_Name;//存储遍历到的Excel文件名

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();//实例化Word对象

if (txt_Word.Text.EndsWith(“\\”))//判断路径是否以\结尾

P_obj_WordName = txt_Word.Text + DateTime.Now.ToString(“yyyyMMddhhmmss”) + “.doc”;//记录Word文件路径及名称

else

P_obj_WordName = txt_Word.Text + “\\” + DateTime.Now.ToString(“yyyyMMddhhmmss”) + “.doc”;//记录Word文件路径及名称

Microsoft.Office.Interop.Word.Table table;//声明Word表格对象

Microsoft.Office.Interop.Word.Document document = new Microsoft.Office.Interop.Word.Document();//声明Word文档对象

document = word.Documents.Add(ref missing, ref missing, ref missing, ref missing);//新建Word文档

Microsoft.Office.Interop.Word.Range range ;//声明范围对象

int P_int_Rows = 0, P_int_Columns = 0;//存储工作表中数据的行数和列数

object P_obj_start = 0, P_obj_end = 0;//分别记录创建表格的开始和结束范围

object P_obj_Range = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;//定义要合并的范围位置

for (int i = 0; i 

{

P_obj_Name = P_str_Names[i];//记录遍历到的Excel文件名

List P_list_SheetName = CBoxBind(P_obj_Name.ToString());//获取指定Excel中的所有工作表

for (int j = 0; j 

{

range = document.Range(ref missing, ref missing);//获取Word范围

range.InsertAfter(P_obj_Name + “——” + P_list_SheetName[j] + “工作表”);//插入文本

range.Font.Name = “宋体”;//设置字体

range.Font.Size = 10;//设置字体大小

DataSet myds = CBoxShowCount(P_obj_Name.ToString(), P_list_SheetName[j]);//获取工作表中的所有数据

P_int_Rows = myds.Tables[0].Rows.Count;//记录工作表的行数

P_int_Columns = myds.Tables[0].Columns.Count;//记录工作表的列数

range.Collapse(ref P_obj_Range);//合并范围

if (P_int_Rows > 0 && P_int_Columns > 0)//判断如果工作表中有记录

{

//在指定范围处添加一个指定行数和列数的表格

table = range.Tables.Add(range, P_int_Rows, P_int_Columns, ref missing, ref missing);

for (int r = 0; r 

{

for (int c = 0; c 

{

table.Cell(r + 1, c + 1).Range.InsertAfter(myds.Tables[0].Rows[r][c].ToString());//将遍历到的数据添加到Word表格中

}

}

}

object P_obj_Format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;//定义Word文档的保存格式

word.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;//设置保存时不显示对话框

//保存Word文档

document.SaveAs(ref P_obj_WordName, ref P_obj_Format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

}

}

document.Close(ref missing, ref missing, ref missing);//关闭Word文档

word.Quit(ref missing, ref missing, ref missing);//退出Word应用程序

MessageBox.Show(“已经成功将多个Excel文件的内容读取到了一个Word文档中!”, “提示”, MessageBoxButtons.OK, MessageBoxIcon.Information);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值