寫出的文件裡面沒內容 是怎麼回事?
new PrintWriter(new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("d:/excel.xls"),
"utf-8"))).write(data);
data代碼:
<?xml version="1.0" encoding="utf-8"?><ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office"><o:DocumentProperties><o:Title>datagrid import to excel</o:Title></o:DocumentProperties><ss:ExcelWorkbook><ss:WindowHeight>9000</ss:WindowHeight><ss:WindowWidth>17000</ss:WindowWidth><ss:ProtectStructure>False</ss:ProtectStructure><ss:ProtectWindows>False</ss:ProtectWindows></ss:ExcelWorkbook><ss:Styles><ss:Style ss:ID="Default"><ss:Alignment ss:Vertical="Top" /><ss:Font ss:FontName="arial" ss:Size="10" /><ss:Borders><ss:Border ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Top" /><ss:Border ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Bottom" /><ss:Border ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Left" /><ss:Border ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Right" /></ss:Borders><ss:Interior /><ss:NumberFormat /><ss:Protection /></ss:Style><ss:Style ss:ID="title"><ss:Borders /><ss:Font /><ss:Alignment ss:Vertical="Center" ss:Horizontal="Center" /><ss:NumberFormat ss:Format="@" /></ss:Style><ss:Style ss:ID="headercell"><ss:Font ss:Bold="1" ss:Size="10" /><ss:Alignment ss:Horizontal="Center" /><ss:Interior ss:Pattern="Solid" /></ss:Style><ss:Style ss:ID="even"><ss:Interior ss:Pattern="Solid" /></ss:Style><ss:Style ss:Parent="even" ss:ID="evendate"><ss:NumberFormat ss:Format="yyyy-mm-dd" /></ss:Style><ss:Style ss:Parent="even" ss:ID="evenint"><ss:NumberFormat ss:Format="0" /></ss:Style><ss:Style ss:Parent="even" ss:ID="evenfloat"><ss:NumberFormat ss:Format="0.00" /></ss:Style><ss:Style ss:ID="odd"><ss:Interior ss:Pattern="Solid" /></ss:Style><ss:Style ss:Parent="odd" ss:ID="odddate"><ss:NumberFormat ss:Format="yyyy-mm-dd" /></ss:Style><ss:Style ss:Parent="odd" ss:ID="oddint"><ss:NumberFormat ss:Format="0" /></ss:Style><ss:Style ss:Parent="odd" ss:ID="oddfloat"><ss:NumberFormat ss:Format="0.00" /></ss:Style></ss:Styles><ss:Worksheet ss:Name="datagrid import to excel"><ss:Names><ss:NamedRange ss:Name="Print_Titles" ss:RefersTo="='datagrid import to excel'!R1:R2" /></ss:Names><ss:Table x:FullRows="1" x:FullColumns="1" ss:ExpandedColumnCount="5" ss:ExpandedRowCount="5"><ss:Column ss:AutoFitWidth="1" ss:Width="130" /><ss:Column ss:AutoFitWidth="1" ss:Width="130" /><ss:Column ss:AutoFitWidth="1" ss:Width="130" /><ss:Row ss:AutoFitHeight="1"><ss:Cell ss:StyleID="headercell"><ss:Data ss:Type="String">ID</ss:Data><ss:NamedCell ss:Name="Print_Titles" /></ss:Cell><ss:Cell ss:StyleID="headercell"><ss:Data ss:Type="String">sex</ss:Data><ss:NamedCell ss:Name="Print_Titles" /></ss:Cell><ss:Cell ss:StyleID="headercell"><ss:Data ss:Type="String">Name</ss:Data><ss:NamedCell ss:Name="Print_Titles" /></ss:Cell></ss:Row><ss:Row><ss:Cell ss:StyleID="even"><ss:Data ss:Type="String">3</ss:Data></ss:Cell><ss:Cell ss:StyleID="even"><ss:Data ss:Type="String">男</ss:Data></ss:Cell><ss:Cell ss:StyleID="even"><ss:Data ss:Type="String">stu5</ss:Data></ss:Cell></ss:Row><ss:Row><ss:Cell ss:StyleID="odd"><ss:Data ss:Type="String">2</ss:Data></ss:Cell><ss:Cell ss:StyleID="odd"><ss:Data ss:Type="String">男</ss:Data></ss:Cell><ss:Cell ss:StyleID="odd"><ss:Data ss:Type="String">stu4</ss:Data></ss:Cell></ss:Row><ss:Row><ss:Cell ss:StyleID="even"><ss:Data ss:Type="String">1</ss:Data></ss:Cell><ss:Cell ss:StyleID="even"><ss:Data ss:Type="String">男</ss:Data></ss:Cell><ss:Cell ss:StyleID="even"><ss:Data ss:Type="String">stu1</ss:Data></ss:Cell></ss:Row></ss:Table><x:WorksheetOptions><x:PageSetup><x:Layout x:CenterHorizontal="1" x:Orientation="Landscape" /><x:Footer x:Data="Page &P of &N" x:Margin="0.5" /><x:PageMargins x:Top="0.5" x:Right="0.5" x:Left="0.5" x:Bottom="0.8" /></x:PageSetup><x:FitToPage /><x:Print><x:PrintErrors>Blank</x:PrintErrors><x:FitWidth>1</x:FitWidth><x:FitHeight>32767</x:FitHeight><x:ValidPrinterInfo /><x:VerticalResolution>600</x:VerticalResolution></x:Print><x:Selected /><x:DoNotDisplayGridlines /><x:ProtectObjects>False</x:ProtectObjects><x:ProtectScenarios>False</x:ProtectScenarios></x:WorksheetOptions></ss:Worksheet></ss:Workbook>
就算你用的是 xlsx 格式,它也是一個 zip 包啊,不是直接一個 xml 啊。