首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VBA >

EXCEL怎么导出工作薄模板中单个工作表

2012-05-28 
EXCEL如何导出工作薄模板中单个工作表请高手指点如何用C#代码将工作薄模板中的单个工作表导出到文件中比如

EXCEL如何导出工作薄模板中单个工作表
请高手指点如何用C#代码将工作薄模板中的单个工作表导出到文件中

比如现在我工作薄模板有20个工作表,如何将其中一个工作表导入数据修改后另存为一个单独的文件。

[解决办法]
翠花,上代码

C# code
        private void copy_excel_sheet(string sFileName, string sSaveName)        {            Excel.Application xlApp1 = new Excel.ApplicationClass();            object missing = Type.Missing;            Excel.Workbook xlSrcBook;            Excel.Workbook xlDestBook;            //xlApp1.Visible = true;            xlApp1.Workbooks.Open(sFileName,                missing, missing, missing, missing, missing, missing, missing,                missing, missing, missing, missing, missing, missing, missing);            xlSrcBook = xlApp1.ActiveWorkbook as Excel.Workbook;            xlDestBook = xlApp1.Workbooks.Add(missing);            xlApp1.DisplayAlerts = false;            xlDestBook.SaveAs(sSaveName,                missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlNoChange,                 missing, missing, missing, missing, missing);            xlApp1.DisplayAlerts = true;            try            {                Excel.Worksheet xlsheet;                xlsheet = (Excel.Worksheet)xlSrcBook.Sheets[2];                xlsheet.Copy(missing, xlDestBook.Sheets[xlDestBook.Sheets.Count]);            }            catch (Exception ex)            {                MessageBox.Show(this, ex.Message);            }            finally            {                xlSrcBook.Close(missing, missing, missing);                xlDestBook.Save();                xlDestBook.Close(missing, missing, missing);                xlApp1.Quit();                xlApp1 = null;            }            MessageBox.Show("Done!");        }
[解决办法]
Sub 导出表到新文件()
'\\作者:宏由 Dnsam 编制,
'\\日期:2010-12-30
'\\作用:将当前表的数据和表格式导出到以当前工作表名称加日期命名的新文件,新文件中不含原表的任何公式和引用

On Error Resume Next
Dim NewN As String '定义要导出的表名称
Dim FN As String '定义新建文件名
Dim ShtN As String '定义新文件工作表表名
Dim PA As String ' 定义导出路径
With ActiveSheet
NewN = .Name '取要导出的工作表名称
Cells.Select '复制整表
Selection.Copy
Range("R1").Select
End With

'\\设置\\\
PA = "C:\Documents and Settings\All Users\Documents\" '导出路径!(ThisWorkbook.Path为当前文件目录)
ShtN = NewN & Left(Date, 10) '新表名
FN = NewN & "_" & Left(Date, 10) & ".xls" '新文件名

'\\选择性复制格式及内容到新的临时工作表
Sheets.Add.Name = ShtN '命名工作表
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False '只粘贴格式
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False '只粘贴值和数字
Application.CutCopyMode = False

'\\设置表格选项,表格格式
With ActiveWindow
.DisplayGridlines = False
.GridlineColorIndex = 31
.DisplayZeros = False
Rows("1:1").RowHeight = 5
Range("A2").Font.Size = 14
End With

'\\打印页面设置:A4横向,页边距
With ActiveSheet.PageSetup
.PaperSize = xlPaperA4
.Orientation = xlLandscape '或.Orientation = 2
.LeftMargin = Application.InchesToPoints(0.55)
.RightMargin = Application.InchesToPoints(0.28)
.TopMargin = Application.InchesToPoints(0.35)
.BottomMargin = Application.InchesToPoints(0.55)
.HeaderMargin = Application.InchesToPoints(0.28)
.FooterMargin = Application.InchesToPoints(0.5)
End With

'\\导出表生成独立的文件(FN)到PA变量指定的路径
Sheets(ShtN).Copy
Range("J18").Select
ActiveWorkbook.SaveAs Filename:=PA & FN
MsgBox ("导出成功!" & vbCrLf & vbCrLf & "已保存到" & PA & vbCrLf & vbCrLf & "文件名:" & FN)


ActiveWorkbook.Close

'\\清理临时工作表
Application.DisplayAlerts = False
Sheets(ShtN).Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
End Sub

热点排行