|
スタイルの変更
フォントや背景色などのスタイルをクライアント側スクリプトにて設定することができます。
クライアント側スクリプトによる設定は保存され、ポストバック後も有効です。
このサンプルでは、選択範囲内のセルのフォントや背景色などをクライアント側スクリプト
にて変更します。
ExcelやPDFのリンクボタンを押下することで、クライアント側スクリプトの変更をそのまま
ExcelファイルやPDFファイルにダウンロードします。
| ID | 氏名 | カナ | 生年月日 | 性別 | 血液型 | 部署 | 入社日 | メールアドレス |
1 | 1001 | 亀甲 滋万 | キコウ シゲマ | 1950/02/04 | 男 | A | 人事部 | 1972/04/01 | sigema_kikou@abc.co.jp |
2 | 1002 | 寒田 希世 | カンダ キヨ | 1959/06/28 | 女 | B | 人事部 | 1981/04/01 | kiyo_kanda@bbb.or.jp |
3 | 1003 | 小和瀬 澄 | オワセ キヨ | 1969/03/06 | 男 | A | 人事部 | 1991/04/01 | kiyo_owase@aaa.co.jp |
4 | 1004 | 宇夫 早余子 | ウブ サヨコ | 1976/07/28 | 女 | O | 人事部 | 1998/04/01 | sayoko_ubu@bbb.or.jp |
5 | 1005 | 宇田津 聖智 | ウダツ キヨトモ | 1965/09/04 | 男 | A | 営業部 | 1987/04/01 | kiyotomo_udatu@abc.co.jp |
6 | 1006 | 茨城 昭児 | イバラキ ショウジ | 1963/04/28 | 男 | O | 営業部 | 1985/04/01 | shouzi_ibaraki@xyz.ne.jp |
7 | 1007 | 石ヶ休 椎茄 | イシガキュウ シイナ | 1953/02/21 | 男 | O | 営業部 | 1975/04/01 | siina_isigagyuu@abc.co.jp |
8 | 1008 | 赤司 恵治郎 | アカツカサ ケイジロウ | 1968/08/02 | 男 | O | 経理部 | 1990/04/01 | keizirou_akatukasa@abc.co.jp |
9 | 1009 | 小橋 仰一 | オハシ ギョウイチ | 1972/03/02 | 男 | B | 経理部 | 1994/04/01 | gyouiti_ohasi@abc.co.jp |
10 | 1010 | 一重 公大 | イチジュウ コウダイ | 1964/04/19 | 男 | B | 経理部 | 1986/04/01 | koudai_itizyuu@xyz.ne.jp |
11 | 1011 | 稲並 勝五郎 | イナミ ショウゴロウ | 1962/02/18 | 男 | A | 営業部 | 1984/04/01 | shougorou_inami@bbb.or.jp |
12 | 1012 | 穎原 紀代一 | エイハラ キヨカズ | 1965/02/13 | 男 | O | 営業部 | 1987/04/01 | kiyokazu_eihara@bbb.or.jp |
|
ソースコード
別ウィンドウで表示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class clientscript_clientsetstyle : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string js = "var spid='" + FpSpread1.ClientID + "';";
ClientScript.RegisterStartupScript(this.GetType(), "onStartUpScript", js, true);
if (IsPostBack)
{
return;
}
// SPREADの設定
InitSpread(FpSpread1);
// シート設定
InitSpreadStyles(FpSpread1.Sheets[0]);
}
private void InitSpread(FarPoint.Web.Spread.FpSpread spread)
{
spread.CssClass = "spreadStyle";
spread.UseClipboard = false;
spread.CommandBar.Visible = false;
// データ連結
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml(MapPath("../App_Data/data.xml"));
spread.DataSource = ds;
}
private void InitSpreadStyles(FarPoint.Web.Spread.SheetView sheet)
{
// フォントサイズの設定
sheet.DefaultStyle.Font.Size = FontUnit.Point(10);
sheet.ColumnHeader.DefaultStyle.Font.Size = FontUnit.Point(10);
sheet.RowHeader.DefaultStyle.Font.Size = FontUnit.Point(10);
sheet.SheetCorner.DefaultStyle.Font.Size = FontUnit.Point(10);
// 列幅の設定
sheet.Columns[0].Width = 36;
sheet.Columns[1].Width = 100;
sheet.Columns[2].Width = 100;
sheet.Columns[3].Width = 80;
sheet.Columns[4].Width = 36;
sheet.Columns[5].Width = 46;
sheet.Columns[6].Width = 49;
sheet.Columns[7].Width = 80;
sheet.Columns[8].Width = 200;
// フォントの設定
sheet.DefaultStyle.Font.Name = "MS ゴシック";
// 縦方向の揃え位置を中央に設定
sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle;
sheet.PageSize = sheet.RowCount;
}
protected void saveExcel_Click(object sender, EventArgs e)
{
// クライアント側の変更を保存
FpSpread1.SaveChanges();
// Excelファイルにエクスポート
FpSpread1.SaveExcelToResponse("clientsetstyle.xls");
}
protected void savePDF_Click(object sender, EventArgs e)
{
// クライアント側の変更を保存
FpSpread1.SaveChanges();
FarPoint.Web.Spread.PrintInfo pi;
pi = new FarPoint.Web.Spread.PrintInfo();
pi.BestFitRows = true;
pi.BestFitCols = true;
FpSpread1.ActiveSheetView.PrintInfo = pi;
// PDFファイルにエクスポート
FpSpread1.SavePdfToResponse("clientsetstyle.pdf");
}
}
|
Partial Class clientscript_clientsetstyle
Inherits System.Web.UI.Page
Protected Sub FpSpread1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles FpSpread1.Load
Dim js As String = "var spid='" + FpSpread1.ClientID & "';"
ClientScript.RegisterStartupScript(Me.GetType(), "onStartUpScript", js, True)
If IsPostBack Then
Return
End If
' SPREADの設定
InitSpread(FpSpread1)
' シート設定
InitSpreadStyles(FpSpread1.Sheets(0))
End Sub
Private Sub InitSpread(ByVal spread As FarPoint.Web.Spread.FpSpread)
spread.CssClass = "spreadStyle"
spread.UseClipboard = False
spread.CommandBar.Visible = False
' データ連結
Dim ds As New System.Data.DataSet()
ds.ReadXml(MapPath("../App_Data/data.xml"))
spread.DataSource = ds
End Sub
Private Sub InitSpreadStyles(ByVal sheet As FarPoint.Web.Spread.SheetView)
' フォントサイズの設定
sheet.DefaultStyle.Font.Size = FontUnit.Point(10)
sheet.ColumnHeader.DefaultStyle.Font.Size = FontUnit.Point(10)
sheet.RowHeader.DefaultStyle.Font.Size = FontUnit.Point(10)
sheet.SheetCorner.DefaultStyle.Font.Size = FontUnit.Point(10)
' 列幅の設定
sheet.Columns(0).Width = 36
sheet.Columns(1).Width = 100
sheet.Columns(2).Width = 100
sheet.Columns(3).Width = 80
sheet.Columns(4).Width = 36
sheet.Columns(5).Width = 46
sheet.Columns(6).Width = 49
sheet.Columns(7).Width = 80
sheet.Columns(8).Width = 200
' フォントの設定
sheet.DefaultStyle.Font.Name = "MS ゴシック"
' 縦方向の揃え位置を中央に設定
sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle
sheet.PageSize = sheet.RowCount
End Sub
Protected Sub saveExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles saveExcel.Click
' クライアント側の変更を保存
FpSpread1.SaveChanges()
' Excelファイルにエクスポート
FpSpread1.SaveExcelToResponse("clientsetstyle.xls")
End Sub
Protected Sub savePDF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles savePDF.Click
' クライアント側の変更を保存
FpSpread1.SaveChanges()
Dim pi As FarPoint.Web.Spread.PrintInfo
pi = New FarPoint.Web.Spread.PrintInfo()
pi.BestFitRows = True
pi.BestFitCols = True
FpSpread1.ActiveSheetView.PrintInfo = pi
' PDFファイルにエクスポート
FpSpread1.SavePdfToResponse("clientsetstyle.pdf")
End Sub
End Class
|
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="clientsetstyle.aspx.cs" Inherits="clientscript_clientsetstyle" %>
<%@ Register Assembly="FarPoint.Web.SpreadJ" Namespace="FarPoint.Web.Spread" TagPrefix="FarPoint" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeaderPlaceHolder1" Runat="Server">
<style type="text/css">
.tbhv
{
font-size: 12px;
}
.tbhv a:hover
{
background-color: #B0C4DE;
}
.toolbar
{
background-image: url(../images/ajax/toolbar.Horizontal.background.gif);
margin-bottom: 1px;
margin-right: 2px;
position: relative;
width: 680px;
overflow: hidden;
}
</style>
<script type="text/javascript">
function pasteData() {
var spread = document.getElementById(spid);
spread.Paste();
}
function btnOver(e) {
var e = e || window.event;
var s = e.target || e.srcElement;
s.style.backgroundColor = "lightsteelblue";
}
function btnOut(e) {
var e = e || window.event;
var s = e.target || e.srcElement;
s.style.backgroundColor = "";
}
function FontBold() {
var spread = document.getElementById(spid);
var rg = spread.GetSelectedRange();
var row;
var col;
if (spread.Cells(rg.row, rg.col).GetFont_Bold()) {
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetFont_Bold(false);
}
}
}
else {
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetFont_Bold(true);
}
}
}
}
function FontItalic() {
var spread = document.getElementById(spid);
var rg = spread.GetSelectedRange();
var row;
var col;
if (spread.Cells(rg.row, rg.col).GetFont_Italic()) {
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetFont_Italic(false);
}
}
}
else {
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetFont_Italic(true);
}
}
}
}
function SetFontName(name) {
var spread = document.getElementById(spid);
var rg = spread.GetSelectedRange();
var row;
var col;
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetFont_Name(name);
}
}
}
function SetFontSize(size) {
var spread = document.getElementById(spid);
var rg = spread.GetSelectedRange();
var row;
var col;
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetFont_Size(size);
}
}
}
function FontUnderline() {
var spread = document.getElementById(spid);
var rg = spread.GetSelectedRange();
var row;
var col;
if (spread.Cells(rg.row, rg.col).GetFont_Underline()) {
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetFont_Underline(false);
}
}
}
else {
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetFont_Underline(true);
}
}
}
}
function AlignLeft() {
var spread = document.getElementById(spid);
var rg = spread.GetSelectedRange();
var row;
var col;
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetHAlign("left");
}
}
}
function AlignCenter() {
var spread = document.getElementById(spid);
var rg = spread.GetSelectedRange();
var row;
var col;
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetHAlign("center");
}
}
}
function AlignRight() {
var spread = document.getElementById(spid);
var rg = spread.GetSelectedRange();
var row;
var col;
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetHAlign("right");
}
}
}
function SetForeColor(color) {
var spread = document.getElementById(spid);
var rg = spread.GetSelectedRange();
var row;
var col;
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetForeColor(color);
}
}
}
function SetBackColor(color) {
var spread = document.getElementById(spid);
var rg = spread.GetSelectedRange();
var row;
var col;
for (row = rg.row; row < rg.row + rg.rowCount; row++) {
for (col = rg.col; col < rg.col + rg.colCount; col++) {
spread.Cells(row, col).SetBackColor(color);
}
}
}
function Cut() {
var spread = document.getElementById(spid);
spread.Clear();
}
function Copy() {
var spread = document.getElementById(spid);
spread.Copy();
}
function Paste() {
var spread = document.getElementById(spid);
spread.Paste();
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="toolbar">
<table id="Table3" cellspacing="0" cellpadding="0" style="border: solid 1px steelblue">
<tr>
<td><img src="../images/ajax/toolbar.Horizontal.start.gif" border="0" alt="" /></td>
<td>
<table id="Table4" cellspacing="0" cellpadding="0" border="0">
<tr>
<td id="ss_btn_Bold" onmouseover="btnOver(event);" onclick="FontBold();"
onmouseout="btnOut(event);" style="height: 22px">
<img height="20" alt="太字" src="../images/ajax/bold.gif" width="21" border="0" />
</td>
<td id="ss_btn_Italic" onmouseover="btnOver(event);" onclick="FontItalic();"
onmouseout="btnOut(event);" style="height: 22px">
<img height="20" alt="斜体" src="../images/ajax/italic.gif" width="21" border="0" />
</td>
<td id="ss_btn_Underline" onmouseover="btnOver(event);" onclick="FontUnderline();"
onmouseout="btnOut(event);" style="height: 22px">
<img height="20" alt="下線" src="../images/ajax/underline.gif" width="21" border="0" />
</td>
<td style="height: 22px">
<img alt="" src="../images/ajax/separator.Horizontal.gif" border="0" />
</td>
<td id="ss_btn_Justify Left" onmouseover="btnOver(event);" onclick="AlignLeft();"
onmouseout="btnOut(event);" style="height: 22px">
<img height="20" alt="左揃え" src="../images/ajax/justifyleft.gif" width="21"
border="0" />
</td>
<td id="ss_btn_Justify Center" onmouseover="btnOver(event);" onclick="AlignCenter();"
onmouseout="btnOut(event);" style="height: 22px">
<img height="20" alt="中央揃え" src="../images/ajax/justifycenter.gif" width="21" border="0" />
</td>
<td id="ss_btn_Justify Right" onmouseover="btnOver(event);" onclick="AlignRight();"
onmouseout="btnOut(event);" style="height: 22px">
<img height="20" alt="右揃え" src="../images/ajax/justifyright.gif" width="21" border="0" />
</td>
<td style="height: 22px">
<img alt="" src="../images/ajax/separator.Horizontal.gif" border="0" />
</td>
<td style="PADDING-LEFT: 4px; height: 22px;">
<select style="WIDTH: 87px" tabindex="-1" onchange="SetFontName(this[this.selectedIndex].text);this.selectedIndex=0;">
<option value="">フォント</option>
<option value="MS ゴシック">MS ゴシック</option>
<option value="MS 明朝">MS 明朝</option>
<option value="MS Pゴシック">MS Pゴシック</option>
<option value="MS P明朝">MS P明朝</option>
</select>
</td>
<td style="PADDING-LEFT: 4px; height: 22px;">
<select style="WIDTH: 64px" tabindex="-1" onchange="SetFontSize(this[this.selectedIndex].text);this.selectedIndex=0;">
<option value="">サイズ</option>
<option value="10px">10px</option>
<option value="11px">11px</option>
<option value="12px">12px</option>
<option value="13px">13px</option>
<option value="17px">17px</option>
<option value="22px">22px</option>
</select>
</td>
<td style="PADDING-LEFT: 4px; height: 22px;">
<select style="WIDTH: 75px" tabindex="-1" onchange="SetForeColor(this[this.selectedIndex].value);this.selectedIndex=0;">
<option value="">文字色</option>
<option style="color: #ffffff; background-color: black" value="#000000">Black</option>
<option style="background-color: #808080" value="#808080">Gray</option>
<option style="background-color: #A9A9A9" value="#A9A9A9">DarkGray</option>
<option style="background-color: #D3D3D3" value="#D3D3D3">LightGray</option>
<option style="background-color: #FFFFFF" value="#FFFFFF">White</option>
<option style="background-color: #7FFFD4" value="#7FFFD4">Aquamarine</option>
<option style="background-color: #0000FF" value="#0000FF">Blue</option>
<option style="color: #ffffff; background-color: #000080" value="#000080">Navy</option>
<option style="color: #ffffff; background-color: #800080" value="#800080">Purple</option>
<option style="background-color: #FF1493" value="#FF1493">DeepPink</option>
<option style="background-color: #EE82EE" value="#EE82EE">Violet</option>
<option style="background-color: #FFC0CB" value="#FFC0CB">Pink</option>
<option style="color: #ffffff; background-color: #006400" value="#006400">DarkGreen</option>
<option style="color: #ffffff; background-color: #008000" value="#008000">Green</option>
<option style="background-color: #9ACD32" value="#9ACD32">YellowGreen</option>
<option style="background-color: #FFFF00" value="#FFFF00">Yellow</option>
<option style="background-color: #FFA500" value="#FFA500">Orange</option>
<option style="background-color: #FF0000" value="#FF0000">Red</option>
<option style="background-color: #A52A2A" value="#A52A2A">Brown</option>
<option style="background-color: #DEB887" value="#DEB887">BurlyWood</option>
<option style="background-color: #F5F5DC" value="#F5F5DC">Beige</option>
</select>
</td>
<td style="PADDING-LEFT: 4px; height: 22px;">
<select style="WIDTH: 85px" tabindex="-1" onchange="SetBackColor(this[this.selectedIndex].value);this.selectedIndex=0;">
<option value="">背景色</option>
<option style="color: #ffffff; background-color: black" value="#000000">Black</option>
<option style="background-color: #808080" value="#808080">Gray</option>
<option style="background-color: #A9A9A9" value="#A9A9A9">DarkGray</option>
<option style="background-color: #D3D3D3" value="#D3D3D3">LightGray</option>
<option style="background-color: #FFFFFF" value="#FFFFFF">White</option>
<option style="background-color: #7FFFD4" value="#7FFFD4">Aquamarine</option>
<option style="background-color: #0000FF" value="#0000FF">Blue</option>
<option style="color: #ffffff; background-color: #000080" value="#000080">Navy</option>
<option style="color: #ffffff; background-color: #800080" value="#800080">Purple</option>
<option style="background-color: #FF1493" value="#FF1493">DeepPink</option>
<option style="background-color: #EE82EE" value="#EE82EE">Violet</option>
<option style="background-color: #FFC0CB" value="#FFC0CB">Pink</option>
<option style="color: #ffffff; background-color: #006400" value="#006400">DarkGreen</option>
<option style="color: #ffffff; background-color: #008000" value="#008000">Green</option>
<option style="background-color: #9ACD32" value="#9ACD32">YellowGreen</option>
<option style="background-color: #FFFF00" value="#FFFF00">Yellow</option>
<option style="background-color: #FFA500" value="#FFA500">Orange</option>
<option style="background-color: #FF0000" value="#FF0000">Red</option>
<option style="background-color: #A52A2A" value="#A52A2A">Brown</option>
<option style="background-color: #DEB887" value="#DEB887">BurlyWood</option>
<option style="background-color: #F5F5DC" value="#F5F5DC">Beige</option>
</select>
</td>
<td style="height: 22px">
<img alt="" src="../images/ajax/separator.Horizontal.gif" border="0" />
</td>
<td id="ss_btn_Cut" onmouseover="btnOver(event);" onclick="Cut();" onmouseout="btnOut(event);" style="height: 22px">
<img height="20" alt="切り取り" src="../images/ajax/cut.gif" width="21" border="0" />
</td>
<td id="ss_btn_Copy" onmouseover="btnOver(event);" onclick="Copy();" onmouseout="btnOut(event);" style="height: 22px">
<img height="20" alt="コピー" src="../images/ajax/copy.gif" width="21" border="0" />
</td>
<td id="ss_btn_Paste" onmouseover="btnOver(event);" onclick="Paste();"
onmouseout="btnOut(event);" style="height: 22px">
<img height="20" alt="貼り付け" src="../images/ajax/paste.gif" width="21" border="0" />
</td>
<td style="PADDING-LEFT: 4px; height: 22px;">
<asp:LinkButton ID="saveExcel" runat="server" CssClass="tbhv"
onclick="saveExcel_Click" onmouseover="btnOver(event);" onmouseout="btnOut(event);" ToolTip="Excelエクスポート">
↓Excel↓
</asp:LinkButton>
</td>
<td style="PADDING-LEFT: 4px; height: 22px;">
<asp:LinkButton ID="savePDF" runat="server" CssClass="tbhv"
onclick="savePDF_Click" onmouseover="btnOver(event);" onmouseout="btnOut(event);" ToolTip="PDFエクスポート">
↓PDF↓
</asp:LinkButton>
</td>
<td style="height: 22px">
<img alt="" src="../images/ajax/separator.Horizontal.gif" border="0" />
</td>
</tr>
</table>
</td>
<td><img alt="" src="../images/ajax/toolbar.Horizontal.end.gif" border="0" /></td>
</tr>
</table>
</div>
<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>
</asp:Content>
|
|
|
|