using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class importexport_saveexcelfile : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { return; } // データ連結 System.Data.DataSet ds = new System.Data.DataSet(); ds.ReadXml(MapPath("../App_Data/data.xml")); FpSpread1.DataSource = ds; // SPREAD初期化 InitSpread(FpSpread1.Sheets[0]); } private void InitSpread(FarPoint.Web.Spread.SheetView sheet) { // SPREAD設定 FpSpread1.CommandBar.Visible = false; FpSpread1.CssClass = "spreadStyle"; FpSpread1.UseClipboard = false; // フォントサイズの設定 sheet.DefaultStyle.Font.Size = FontUnit.Point(9); sheet.ColumnHeader.DefaultStyle.Font.Size = FontUnit.Point(9); sheet.RowHeader.DefaultStyle.Font.Size = FontUnit.Point(9); sheet.SheetCorner.DefaultStyle.Font.Size = FontUnit.Point(9); // シート設定 sheet.PageSize = sheet.RowCount; sheet.Protect = false; // 列幅の設定 sheet.Columns[0].Width = 36; sheet.Columns[1].Width = 88; sheet.Columns[2].Width = 91; sheet.Columns[3].Width = 80; sheet.Columns[4].Width = 36; sheet.Columns[5].Width = 55; sheet.Columns[6].Width = 49; sheet.Columns[7].Width = 80; sheet.Columns[8].Width = 181; // 縦方向の揃え位置を中央に設定 sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle; // 6列目(血液型)にフィルタリングの設定をします FarPoint.Web.Spread.HideRowFilter hf = new FarPoint.Web.Spread.HideRowFilter(sheet); FarPoint.Web.Spread.FilterColumnDefinition fcd = new FarPoint.Web.Spread.FilterColumnDefinition(5); hf.AddColumn(fcd); sheet.RowFilter = hf; } protected void Button1_Click(object sender, EventArgs e) { // クライアント側の変更を確定 FpSpread1.SaveChanges(); string filename; FarPoint.Excel.ExcelSaveFlags saveFlg = FarPoint.Excel.ExcelSaveFlags.NoFlagsSet; // 保存形式に関する設定 if (DropDownList1.SelectedValue == "xls") { filename = "spread.xls"; } else { filename = "spread.xlsx"; saveFlg = saveFlg | FarPoint.Excel.ExcelSaveFlags.UseOOXMLFormat; } // チェックボックス選択状態に関連する設定 if (CheckBox1.Checked) { saveFlg = saveFlg | FarPoint.Excel.ExcelSaveFlags.SaveAsFiltered; } if (CheckBox2.Checked) { saveFlg = saveFlg | FarPoint.Excel.ExcelSaveFlags.SaveCustomColumnHeaders; } // 保存を実行 FpSpread1.SaveExcelToResponse(filename, saveFlg); } }
Partial Class importexport_saveexcelfile Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If IsPostBack Then Return End If ' データ連結 Dim ds As New System.Data.DataSet() ds.ReadXml(MapPath("../App_Data/data.xml")) FpSpread1.DataSource = ds ' SPREAD初期化 InitSpread(FpSpread1.Sheets(0)) End Sub Private Sub InitSpread(sheet As FarPoint.Web.Spread.SheetView) ' SPREAD設定 FpSpread1.CommandBar.Visible = False FpSpread1.CssClass = "spreadStyle" FpSpread1.UseClipboard = False ' フォントサイズの設定 sheet.DefaultStyle.Font.Size = FontUnit.Point(9) sheet.ColumnHeader.DefaultStyle.Font.Size = FontUnit.Point(9) sheet.RowHeader.DefaultStyle.Font.Size = FontUnit.Point(9) sheet.SheetCorner.DefaultStyle.Font.Size = FontUnit.Point(9) ' シート設定 sheet.PageSize = sheet.RowCount sheet.Protect = False ' 列幅の設定 sheet.Columns(0).Width = 36 sheet.Columns(1).Width = 88 sheet.Columns(2).Width = 91 sheet.Columns(3).Width = 80 sheet.Columns(4).Width = 36 sheet.Columns(5).Width = 55 sheet.Columns(6).Width = 49 sheet.Columns(7).Width = 80 sheet.Columns(8).Width = 181 ' 縦方向の揃え位置を中央に設定 sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle ' 6列目(血液型)にフィルタリングの設定をします Dim hf As New FarPoint.Web.Spread.HideRowFilter(sheet) Dim fcd As New FarPoint.Web.Spread.FilterColumnDefinition(5) hf.AddColumn(fcd) sheet.RowFilter = hf End Sub Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click ' クライアント側の変更を確定 FpSpread1.SaveChanges() Dim filename As String Dim saveFlg As FarPoint.Excel.ExcelSaveFlags = FarPoint.Excel.ExcelSaveFlags.NoFlagsSet ' 保存形式に関する設定 If DropDownList1.SelectedValue = "xls" Then filename = "spread.xls" Else filename = "spread.xlsx" saveFlg = saveFlg Or FarPoint.Excel.ExcelSaveFlags.UseOOXMLFormat End If ' チェックボックス選択状態に関連する設定 If CheckBox1.Checked Then saveFlg = saveFlg Or FarPoint.Excel.ExcelSaveFlags.SaveAsFiltered End If If CheckBox2.Checked Then saveFlg = saveFlg Or FarPoint.Excel.ExcelSaveFlags.SaveCustomColumnHeaders End If ' 保存を実行 FpSpread1.SaveExcelToResponse(filename, saveFlg) End Sub End Class
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="saveexcelfile.aspx.cs" Inherits="importexport_saveexcelfile" %> <%@ Register Assembly="FarPoint.Web.SpreadJ" Namespace="FarPoint.Web.Spread" TagPrefix="FarPoint" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeaderPlaceHolder1" runat="Server"> <style type="text/css"> .Exdiv { margin-top: 10px; } </style> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <FarPoint:FpSpread ID="FpSpread1" runat="server" BorderColor="#A0A0A0" BorderStyle="Solid" BorderWidth="1px"> <CommandBar BackColor="#F6F6F6" ButtonFaceColor="Control" ButtonHighlightColor="ControlLightLight" ButtonShadowColor="ControlDark"> </CommandBar> <Sheets> <FarPoint:SheetView SheetName="Sheet1"> </FarPoint:SheetView> </Sheets> </FarPoint:FpSpread> <div class="Exdiv"> <asp:Label ID="Label1" runat="server" Text="ファイルの種類"></asp:Label> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Value="xls">BIFF8(xls)</asp:ListItem> <asp:ListItem Value="xlsx">Open XML(xlsx)</asp:ListItem> </asp:DropDownList> <asp:CheckBox ID="CheckBox1" runat="server" Style="margin-left: 10px" Text="フィルター結果をそのまま" /> <asp:CheckBox ID="CheckBox2" runat="server" Style="margin-left: 10px" Text="ヘッダを含める" /> </div> <asp:Button ID="Button1" runat="server" Style="margin-top: 10px" Width="200px" Height="30px" Text="Excelファイルへエクスポート" OnClick="Button1_Click" /> </asp:Content>