|
csv(テキスト)ファイルの読込
インポートボタンを押下すると、csv(テキスト)ファイルをSPREADへロードします。
クリアボタンを押下するとSPREADのデータがクリアされます。
ヘッダの要否、行・列の区切り文字、セルの区切り文字をパラメータにより設定可能です。
ソースコード
別ウィンドウで表示
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
public partial class opentextfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
// 初期化
FpSpread1.CommandBar.Visible = false;
FpSpread1.CssClass = "spreadStyle";
}
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.Columns[0].Width = 35;
sheet.Columns[1].Width = 80;
sheet.Columns[2].Width = 90;
sheet.Columns[3].Width = 68;
sheet.Columns[4].Width = 40;
sheet.Columns[5].Width = 38;
sheet.Columns[6].Width = 50;
sheet.Columns[7].Width = 70;
sheet.Columns[8].Width = 173;
// 縦方向の揃え位置を中央に設定
sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle;
}
protected void Button1_Click(object sender, EventArgs e)
{
// クライアント側の変更を確定
FpSpread1.SaveChanges();
string filepath = Server.MapPath("~/App_Data/spread.csv");
try
{
// CSVファイルからインポート
FpSpread1.Sheets[0].LoadTextFile(filepath, true, FarPoint.Web.Spread.Model.IncludeHeaders.ColumnHeadersCustomOnly, "\r\n", ",", "\"");
InitSpread(FpSpread1.Sheets[0]);
}
catch (System.Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
protected void Button2_Click(object sender, EventArgs e)
{
// クライアント側の変更を確定
FpSpread1.SaveChanges();
// Spreadコントロールをクリア
FpSpread1.Reset();
// 初期化
FpSpread1.CommandBar.Visible = false;
FpSpread1.CssClass = "spreadStyle";
FarPoint.Web.Spread.SheetView wkSheet = new FarPoint.Web.Spread.SheetView();
FpSpread1.Sheets.Add(wkSheet);
}
protected void btnDownLoad_Click(object sender, EventArgs e)
{
// クライアント側の変更を確定
FpSpread1.SaveChanges();
string filepath = Server.MapPath("~/App_Data/spread.csv");
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=spread.csv");
Response.Flush();
Response.WriteFile(filepath);
Response.End();
}
}
|
Partial Public Class opentextfile
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
Return
End If
' 初期化
FpSpread1.CommandBar.Visible = False
FpSpread1.CssClass = "spreadStyle"
End Sub
Private Sub InitSpread(ByVal 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.Columns(0).Width = 35
sheet.Columns(1).Width = 80
sheet.Columns(2).Width = 90
sheet.Columns(3).Width = 68
sheet.Columns(4).Width = 40
sheet.Columns(5).Width = 38
sheet.Columns(6).Width = 50
sheet.Columns(7).Width = 70
sheet.Columns(8).Width = 173
' 縦方向の揃え位置を中央に設定
sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
' クライアント側の変更を確定
FpSpread1.SaveChanges()
Dim filepath As String = Server.MapPath("~/App_Data/spread.csv")
Try
' CSVファイルからインポート
FpSpread1.Sheets(0).LoadTextFile(filepath, True, FarPoint.Web.Spread.Model.IncludeHeaders.ColumnHeadersCustomOnly, vbCr & vbLf, ",", """")
InitSpread(FpSpread1.Sheets(0))
Catch ex As System.Exception
Response.Write(ex.Message.ToString())
End Try
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
' クライアント側の変更を確定
FpSpread1.SaveChanges()
' Spreadコントロールをクリア
FpSpread1.Reset()
' 初期化
FpSpread1.CommandBar.Visible = False
FpSpread1.CssClass = "spreadStyle"
Dim wkSheet As New FarPoint.Web.Spread.SheetView()
FpSpread1.Sheets.Add(wkSheet)
End Sub
Protected Sub btnDownLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownLoad.Click
' クライアント側の変更を確定
FpSpread1.SaveChanges()
Dim filepath As String = Server.MapPath("~/App_Data/spread.csv")
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment;filename=spread.csv")
Response.Flush()
Response.WriteFile(filepath)
Response.End()
End Sub
End Class
|
<%@ Page MasterPageFile="~/MasterPage.master" Language="c#" AutoEventWireup="true"
Inherits="opentextfile" CodeFile="opentextfile.aspx.cs" %>
<%@ Register Assembly="FarPoint.Web.SpreadJ" Namespace="FarPoint.Web.Spread" TagPrefix="FarPoint" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server" Text="インポート" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="クリア" onclick="Button2_Click" />
<asp:Button ID="btnDownLoad" runat="server" Text="インポートファイルのダウンロード"
onclick="btnDownLoad_Click"/>
<farpoint:FpSpread ID="FpSpread1" runat="server" BorderColor="#A0A0A0" BorderStyle="Solid"
BorderWidth="1px">
<CommandBar BackColor="#F6F6F6" ButtonFaceColor="Control" ButtonHighlightColor="ControlLightLight"
ButtonShadowColor="ControlDark" />
<Sheets>
<farpoint:SheetView SheetName="Sheet1">
</farpoint:SheetView>
</Sheets>
</farpoint:FpSpread>
</asp:Content>
|
|
|
|